@confighub/api 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,39 @@
1
+ import createClient from 'openapi-fetch';
2
+
3
+ // src/client.ts
4
+ var trimTrailingSlash = (s) => s.replace(/\/+$/, "");
5
+ var apiBaseUrl = (baseUrl) => {
6
+ const trimmed = trimTrailingSlash(baseUrl);
7
+ return trimmed.endsWith("/api") ? trimmed : trimmed + "/api";
8
+ };
9
+ function createConfigHubClient(options) {
10
+ const { baseUrl, getToken, onUnauthorized, fetch: fetchImpl } = options;
11
+ const client = createClient({
12
+ baseUrl: apiBaseUrl(baseUrl),
13
+ fetch: fetchImpl
14
+ });
15
+ const middleware = {
16
+ async onRequest({ request }) {
17
+ if (getToken) {
18
+ const token = await getToken();
19
+ if (token) request.headers.set("Authorization", `Bearer ${token}`);
20
+ }
21
+ if (request.method === "PATCH" && request.body != null) {
22
+ request.headers.set("Content-Type", "application/merge-patch+json");
23
+ }
24
+ return request;
25
+ },
26
+ async onResponse({ response }) {
27
+ if (response.status === 401 && onUnauthorized) {
28
+ await onUnauthorized();
29
+ }
30
+ return response;
31
+ }
32
+ };
33
+ client.use(middleware);
34
+ return client;
35
+ }
36
+
37
+ export { createConfigHubClient };
38
+ //# sourceMappingURL=index.js.map
39
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/client.ts"],"names":[],"mappings":";;;AAqCA,IAAM,oBAAoB,CAAC,CAAA,KAAsB,CAAA,CAAE,OAAA,CAAQ,QAAQ,EAAE,CAAA;AAIrE,IAAM,UAAA,GAAa,CAAC,OAAA,KAA4B;AAC9C,EAAA,MAAM,OAAA,GAAU,kBAAkB,OAAO,CAAA;AACzC,EAAA,OAAO,OAAA,CAAQ,QAAA,CAAS,MAAM,CAAA,GAAI,UAAU,OAAA,GAAU,MAAA;AACxD,CAAA;AAaO,SAAS,sBAAsB,OAAA,EAAkD;AACtF,EAAA,MAAM,EAAE,OAAA,EAAS,QAAA,EAAU,cAAA,EAAgB,KAAA,EAAO,WAAU,GAAI,OAAA;AAEhE,EAAA,MAAM,SAAS,YAAA,CAAoB;AAAA,IACjC,OAAA,EAAS,WAAW,OAAO,CAAA;AAAA,IAC3B,KAAA,EAAO;AAAA,GACR,CAAA;AAED,EAAA,MAAM,UAAA,GAAyB;AAAA,IAC7B,MAAM,SAAA,CAAU,EAAE,OAAA,EAAQ,EAAG;AAC3B,MAAA,IAAI,QAAA,EAAU;AACZ,QAAA,MAAM,KAAA,GAAQ,MAAM,QAAA,EAAS;AAC7B,QAAA,IAAI,OAAO,OAAA,CAAQ,OAAA,CAAQ,IAAI,eAAA,EAAiB,CAAA,OAAA,EAAU,KAAK,CAAA,CAAE,CAAA;AAAA,MACnE;AAIA,MAAA,IAAI,OAAA,CAAQ,MAAA,KAAW,OAAA,IAAW,OAAA,CAAQ,QAAQ,IAAA,EAAM;AACtD,QAAA,OAAA,CAAQ,OAAA,CAAQ,GAAA,CAAI,cAAA,EAAgB,8BAA8B,CAAA;AAAA,MACpE;AACA,MAAA,OAAO,OAAA;AAAA,IACT,CAAA;AAAA,IACA,MAAM,UAAA,CAAW,EAAE,QAAA,EAAS,EAAG;AAC7B,MAAA,IAAI,QAAA,CAAS,MAAA,KAAW,GAAA,IAAO,cAAA,EAAgB;AAC7C,QAAA,MAAM,cAAA,EAAe;AAAA,MACvB;AACA,MAAA,OAAO,QAAA;AAAA,IACT;AAAA,GACF;AAEA,EAAA,MAAA,CAAO,IAAI,UAAU,CAAA;AACrB,EAAA,OAAO,MAAA;AACT","file":"index.js","sourcesContent":["// Copyright (C) ConfigHub, Inc.\n// SPDX-License-Identifier: MIT\n\nimport createClient, { type Client, type Middleware } from 'openapi-fetch';\nimport type { paths } from './schema';\n\nexport interface ConfigHubClientOptions {\n /**\n * Base URL of the ConfigHub instance, e.g. `https://hub.confighub.com` — the same\n * value passed to the auth provider. The ConfigHub API is mounted under `/api`\n * (the spec's paths are relative to it), so the client targets `{baseUrl}/api`.\n * Passing a URL that already ends in `/api` is accepted as-is.\n */\n baseUrl: string;\n\n /**\n * Returns the current bearer token, or undefined when unauthenticated. May be\n * async so the caller can await a refresh. Wired as an openapi-fetch middleware\n * that sets `Authorization: Bearer <token>` per request. The client never stores\n * or refreshes tokens itself — that is the auth layer's job (see\n * `@confighub/react-auth`).\n */\n getToken?: () => string | undefined | Promise<string | undefined>;\n\n /**\n * Called when a request returns 401. A library must not redirect on its own, so\n * this hands control back to the caller — typically to trigger a token refresh\n * or re-login. The originating request is not retried automatically.\n */\n onUnauthorized?: () => void | Promise<void>;\n\n /** Override the fetch implementation (tests, non-browser runtimes). */\n fetch?: typeof globalThis.fetch;\n}\n\nexport type ConfigHubClient = Client<paths>;\n\nconst trimTrailingSlash = (s: string): string => s.replace(/\\/+$/, '');\n\n// The ConfigHub API lives under /api and the OpenAPI paths are relative to it. Accept\n// either the instance origin or a URL that already includes /api.\nconst apiBaseUrl = (baseUrl: string): string => {\n const trimmed = trimTrailingSlash(baseUrl);\n return trimmed.endsWith('/api') ? trimmed : trimmed + '/api';\n};\n\n/**\n * Create a typed ConfigHub API client. Every path, param, and response is derived\n * from the pinned OpenAPI spec (`src/schema.d.ts`), so it stays in lockstep with\n * the server.\n *\n * ```ts\n * const api = createConfigHubClient({ baseUrl, getToken: () => session.accessToken });\n * const { data, error } = await api.GET('/me');\n * const units = await api.GET('/space/{space_id}/unit', { params: { path: { space_id } } });\n * ```\n */\nexport function createConfigHubClient(options: ConfigHubClientOptions): ConfigHubClient {\n const { baseUrl, getToken, onUnauthorized, fetch: fetchImpl } = options;\n\n const client = createClient<paths>({\n baseUrl: apiBaseUrl(baseUrl),\n fetch: fetchImpl,\n });\n\n const middleware: Middleware = {\n async onRequest({ request }) {\n if (getToken) {\n const token = await getToken();\n if (token) request.headers.set('Authorization', `Bearer ${token}`);\n }\n // ConfigHub's PATCH endpoints expect RFC 7386 merge-patch semantics. This\n // mirrors the first-party UI, which sets the same content type for its\n // patch/bulk-patch operations.\n if (request.method === 'PATCH' && request.body != null) {\n request.headers.set('Content-Type', 'application/merge-patch+json');\n }\n return request;\n },\n async onResponse({ response }) {\n if (response.status === 401 && onUnauthorized) {\n await onUnauthorized();\n }\n return response;\n },\n };\n\n client.use(middleware);\n return client;\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@confighub/api",
3
+ "version": "0.1.0",
4
+ "description": "Typed, framework-agnostic client for the ConfigHub API",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/confighub/js-sdk.git",
9
+ "directory": "packages/api"
10
+ },
11
+ "homepage": "https://github.com/confighub/js-sdk/tree/main/packages/api#readme",
12
+ "bugs": "https://github.com/confighub/js-sdk/issues",
13
+ "type": "module",
14
+ "main": "./dist/index.cjs",
15
+ "module": "./dist/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "import": {
20
+ "types": "./dist/index.d.ts",
21
+ "default": "./dist/index.js"
22
+ },
23
+ "require": {
24
+ "types": "./dist/index.d.cts",
25
+ "default": "./dist/index.cjs"
26
+ }
27
+ }
28
+ },
29
+ "files": [
30
+ "dist",
31
+ "README.md",
32
+ "LICENSE"
33
+ ],
34
+ "sideEffects": false,
35
+ "scripts": {
36
+ "build": "tsup",
37
+ "dev": "tsup --watch",
38
+ "typecheck": "tsc --noEmit",
39
+ "prepublishOnly": "npm run build"
40
+ },
41
+ "dependencies": {
42
+ "openapi-fetch": "^0.13.1"
43
+ },
44
+ "keywords": [
45
+ "confighub",
46
+ "openapi",
47
+ "api-client",
48
+ "typescript"
49
+ ],
50
+ "publishConfig": {
51
+ "access": "public"
52
+ }
53
+ }