From d1a8c9c5d9c8a628162455a99e075e1f591a27e9 Mon Sep 17 00:00:00 2001 From: Sergey Rymsha Date: Wed, 1 Jul 2026 19:21:18 +0000 Subject: [PATCH] Add @enonic-types/lib-http-client types package #226 Adds a published TypeScript types package for lib-http-client so consumers no longer hand-maintain local declarations for /lib/http-client. - types/index.d.ts: ambient module declaration for /lib/http-client (single request() export). Types verified against the JS bridge and the Java bean; corrects a handful of drifts from the app-features copy (connectTimeout -> connectionTimeout, readTimeout number, body accepts ByteSource, response cookies as an array of cookie objects, headers as string|string[], plus previously-missing queryParams, disableHttp2, multipart, followRedirects, certificates, clientCertificate, bodyStream). - types/package.json: @enonic-types/lib-http-client manifest. - build.gradle: assembleTypes Copy task -> build/types (version substituted from project.version); jar.dependsOn assembleTypes. - workflow: npmPublish: true on build-and-publish + id-token permission for OIDC trusted publishing. Closes #226 Co-Authored-By: Claude Opus 4.7 (1M context) --- .github/workflows/enonic-gradle.yml | 5 + build.gradle | 12 ++ types/index.d.ts | 236 ++++++++++++++++++++++++++++ types/package.json | 33 ++++ 4 files changed, 286 insertions(+) create mode 100644 types/index.d.ts create mode 100644 types/package.json diff --git a/.github/workflows/enonic-gradle.yml b/.github/workflows/enonic-gradle.yml index f4853a2..2158e54 100644 --- a/.github/workflows/enonic-gradle.yml +++ b/.github/workflows/enonic-gradle.yml @@ -2,6 +2,10 @@ name: Gradle Build on: [ push ] +permissions: + id-token: write # Required for npm OIDC / trusted publishing + contents: write + jobs: build: runs-on: ubuntu-latest @@ -13,6 +17,7 @@ jobs: with: repoUser: ci repoPassword: ${{ secrets.ARTIFACTORY_PASSWORD }} + npmPublish: true releaseBranch: | master 3.x diff --git a/build.gradle b/build.gradle index 5998009..872b36e 100644 --- a/build.gradle +++ b/build.gradle @@ -46,3 +46,15 @@ check.dependsOn jacocoTestReport artifacts { archives jar } + +// Assemble the @enonic-types/lib-http-client npm package (published on release by the CI npmPublish step). +tasks.register('assembleTypes', Copy) { + from 'types' + into layout.buildDirectory.dir('types') + filesMatching('package.json') { + filter { line -> + line.replaceAll(/"version":\s*"[^"]*"/, "\"version\": \"${project.version}\"") + } + } +} +jar.dependsOn assembleTypes diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..5f8868a --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,236 @@ +import type { ByteSource } from "@enonic-types/core"; + +declare module "/lib/http-client" { + export interface HttpRequestAuth { + /** + * User name for basic authentication. + */ + user?: string; + + /** + * Password for basic authentication. + */ + password?: string; + } + + export interface HttpRequestProxy { + /** + * Proxy host name or IP address to use. + */ + host?: string; + + /** + * Proxy port to use. + */ + port?: number; + + /** + * User name for proxy authentication. + */ + user?: string; + + /** + * Password for proxy authentication. + */ + password?: string; + } + + export interface HttpRequestMultipartPart { + /** + * Name of the part. + */ + name: string; + + /** + * Value of the part. Can be a string or a stream. + */ + value: string | ByteSource; + + /** + * File name of the part. + */ + fileName?: string; + + /** + * Content type of the part. + */ + contentType?: string; + } + + export interface HttpRequestParams { + /** + * URL to which the request is sent. + */ + url: string; + + /** + * The HTTP method to use for the request (e.g. `"POST"`, `"GET"`, `"HEAD"`, `"PUT"`, `"DELETE"`, `"PATCH"`). + * + * @default "GET" + */ + method?: string; + + /** + * Query parameters to be sent with the request. + */ + queryParams?: Record; + + /** + * Body form parameters. Will be encoded according to `application/x-www-form-urlencoded`. + * For `GET` and `HEAD` request methods params are added to query string, but only if `queryParams` is not provided. + */ + params?: Record; + + /** + * HTTP headers, an object where the keys are header names and the values the header values. + */ + headers?: Record; + + /** + * Disable use of HTTP/2 protocol. For insecure HTTP connections HTTP/2 is always disabled. + * + * @default false + */ + disableHttp2?: boolean; + + /** + * The timeout on establishing the connection, in milliseconds. + * + * @default 10000 + */ + connectionTimeout?: number; + + /** + * The timeout on waiting to receive data, in milliseconds. + * + * @default 10000 + */ + readTimeout?: number; + + /** + * Body content to send with the request, usually for `POST` or `PUT` requests. + * Can be a string or a stream. + */ + body?: string | ByteSource; + + /** + * Content type of the request. Only applicable for requests with a body or multipart. + */ + contentType?: string; + + /** + * Multipart form data to send with the request, as an array of part objects. + */ + multipart?: HttpRequestMultipartPart[]; + + /** + * Settings for basic authentication. + */ + auth?: HttpRequestAuth; + + /** + * Proxy settings. + */ + proxy?: HttpRequestProxy; + + /** + * If set to `false`, redirect responses (`status=3xx`) will not trigger a new internal request, + * and the function will return directly with the `3xx` status. + * If `true`, redirects will be handled internally. Default is to handle redirects internally, + * but not redirect from https to http. + */ + followRedirects?: boolean; + + /** + * Stream of PEM encoded certificates. Replaces the host platform's certificate authorities with a custom set. + */ + certificates?: ByteSource; + + /** + * Stream is interpreted as PEM encoded certificate: private key (in PKCS #8 format) and the client certificate concatenated. + */ + clientCertificate?: ByteSource; + } + + export interface HttpResponseCookie { + /** + * Cookie name. + */ + name: string; + + /** + * Cookie value. + */ + value: string; + + /** + * Cookie path. + */ + path: string | null; + + /** + * Cookie domain. + */ + domain: string | null; + + /** + * Cookie expiration as epoch milliseconds, or `null` when the cookie is a session cookie. + */ + expires: number | null; + + /** + * Whether the cookie is only sent over secure connections. + */ + secure: boolean; + + /** + * Whether the cookie is inaccessible to client-side scripts. + */ + httpOnly: boolean; + } + + export interface HttpResponse { + /** + * HTTP status code returned. + */ + status: number; + + /** + * HTTP status message returned. + */ + message: string; + + /** + * HTTP headers of the response. A value is a `string` for single-valued headers and a `string[]` for repeated headers. + */ + headers: Record; + + /** + * Content type of the response. + */ + contentType: string | null; + + /** + * Body of the response as a string. `null` if the response content-type is not of type text. + */ + body: string | null; + + /** + * Body of the response as a stream. + */ + bodyStream: ByteSource; + + /** + * HTTP cookies set in the response. + */ + cookies: HttpResponseCookie[]; + } + + /** + * Sends an HTTP request and returns the response received from the remote server. + * The request is sent synchronously — execution blocks until the response is received. + */ + export function request(params: HttpRequestParams): HttpResponse; +} + +export {}; diff --git a/types/package.json b/types/package.json new file mode 100644 index 0000000..6682fae --- /dev/null +++ b/types/package.json @@ -0,0 +1,33 @@ +{ + "name": "@enonic-types/lib-http-client", + "version": "0.0.0", + "description": "Type definitions for the Enonic XP HTTP Client library", + "types": "index.d.ts", + "files": [ + "*" + ], + "keywords": [ + "enonic", + "enonic-xp", + "lib-http-client", + "http", + "http-client", + "types", + "typescript" + ], + "license": "Apache-2.0", + "homepage": "https://github.com/enonic/lib-http-client#readme", + "repository": { + "type": "git", + "url": "git+https://github.com/enonic/lib-http-client.git" + }, + "bugs": { + "url": "https://github.com/enonic/lib-http-client/issues" + }, + "publishConfig": { + "access": "public" + }, + "dependencies": { + "@enonic-types/core": "^8.0.0" + } +}