@unshared/client 0.2.2 → 0.2.3
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.
@@ -22,9 +22,8 @@ declare namespace OpenAPIV2 {
|
|
22
22
|
[K in keyof P]?: InferSchema<P[K]>;
|
23
23
|
} : Record<string, unknown>;
|
24
24
|
type InferSchemaArray<T> = T extends {
|
25
|
-
items
|
26
|
-
|
27
|
-
additionalItems: infer U;
|
25
|
+
items?: infer U;
|
26
|
+
additionalItems?: infer U;
|
28
27
|
} ? Array<InferSchema<U>> : unknown[];
|
29
28
|
type InferSchema<T> = Pretty<(T extends {
|
30
29
|
anyOf: Array<infer U>;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"createClient.cjs","sources":["../createClient.ts"],"sourcesContent":["import type { MaybeLiteral, Override, Pretty } from '@unshared/types'\nimport type { OpenAPI, OpenAPIV2 as V2, OpenAPIV3 as V3, OpenAPIV3_1 as V3_1 } from 'openapi-types'\nimport type { OpenAPIV2, OpenAPIV3 } from './openapi/index'\nimport type { RequestHooks } from './utils/handleResponse'\nimport type {
|
1
|
+
{"version":3,"file":"createClient.cjs","sources":["../createClient.ts"],"sourcesContent":["import type { MaybeLiteral, Override, Pretty } from '@unshared/types'\nimport type { OpenAPI, OpenAPIV2 as V2, OpenAPIV3 as V3, OpenAPIV3_1 as V3_1 } from 'openapi-types'\nimport type { OpenAPIV2, OpenAPIV3 } from './openapi/index'\nimport type { RequestHooks } from './utils/handleResponse'\nimport type { RequestMethod, RequestOptions } from './utils/parseRequest'\nimport { fetch } from './fetch'\nimport { getBaseUrl } from './openapi/getBaseUrl'\nimport { getOperationById } from './openapi/getOperationById'\nimport { handleResponse } from './utils/handleResponse'\n\ntype ClientBaseUrl<T> =\n MaybeLiteral<\n T extends V2.Document ? OpenAPIV2.ServerUrl<T>\n : T extends V3.Document ? OpenAPIV3.ServerUrl<T>\n : T extends V3_1.Document ? OpenAPIV3.ServerUrl<T>\n : string\n >\n\ntype ClientFetch<T> =\n T extends V2.Document ? <P extends OpenAPIV2.Route<T>>(name: P, options: OpenAPIV2.RequestInit<T, OpenAPIV2.OperationByRoute<T, P>>) => Promise<OpenAPIV2.Response<OpenAPIV2.OperationByRoute<T, P>>>\n : T extends V3.Document ? <P extends OpenAPIV2.Route<T>>(name: P, options: OpenAPIV3.RequestInit<T, OpenAPIV2.OperationByRoute<T, P>>) => Promise<OpenAPIV3.Response<OpenAPIV2.OperationByRoute<T, P>>>\n : T extends V3_1.Document ? <P extends OpenAPIV2.Route<T>>(name: P, options: OpenAPIV3.RequestInit<T, OpenAPIV2.OperationByRoute<T, P>>) => Promise<OpenAPIV3.Response<OpenAPIV2.OperationByRoute<T, P>>>\n : typeof globalThis.fetch\n\ntype ClientFetchOperation<T, U extends OpenAPIV2.OperationId<T>> =\n T extends V2.Document ? (options: OpenAPIV2.RequestInit<T, OpenAPIV2.OperationById<T, U>>) => Promise<OpenAPIV2.ResponseBody<OpenAPIV2.OperationById<T, U>>>\n : T extends V3.Document ? (options: OpenAPIV3.RequestInit<T, OpenAPIV2.OperationById<T, U>>) => Promise<OpenAPIV3.ResponseBody<OpenAPIV2.OperationById<T, U>>>\n : T extends V3_1.Document ? (options: OpenAPIV3.RequestInit<T, OpenAPIV2.OperationById<T, U>>) => Promise<OpenAPIV3.ResponseBody<OpenAPIV2.OperationById<T, U>>>\n : (options: RequestOptions) => Promise<Response>\n\nexport type Client<T = OpenAPI.Document> =\n Pretty<\n & { [K in OpenAPIV2.OperationId<T>]: ClientFetchOperation<T, K> }\n & { fetch: ClientFetch<T> }\n >\n\nexport type ClientOptions<T = any> = Override<RequestHooks & RequestOptions, {\n baseUrl?: ClientBaseUrl<T>\n\n /**\n * The headers to include in every request made by the client.\n *\n * @example { 'Authorization': 'Bearer ...' }\n */\n headers?: T extends V3.Document\n ? OpenAPIV3.ServerHeaders<T>\n : never\n}>\n\n/**\n * Create a new client instance for the given OpenAPI specification.\n *\n * @param document The OpenAPI specification document.\n * @param initialOptions The initial options to use for every request.\n * @returns The client instance.\n * @example\n * const client = createClient(document)\n * await client.fetch({ ... })\n */\n// @ts-expect-error: `ClientOptions` is not assignable to `ClientOptions<T>`.\nexport function createClient<T extends OpenAPI.Document>(document: Readonly<T>, initialOptions?: ClientOptions<T>): Client<T>\nexport function createClient<T extends OpenAPI.Document>(url: ClientBaseUrl<T>, initialOptions?: ClientOptions<T>): Client<T>\nexport function createClient(documentOrUrl: Readonly<OpenAPI.Document> | string, initialOptions: ClientOptions = {}): Client {\n const specifications = typeof documentOrUrl === 'string' ? undefined : documentOrUrl\n if (typeof documentOrUrl === 'string') initialOptions.baseUrl = documentOrUrl\n\n async function fetchByOperationId(operationId: string, options: ClientOptions<any>) {\n if (!specifications) throw new Error('No OpenAPI specification provided.')\n const operation = getOperationById(specifications, operationId) as { method: RequestMethod; path: string } & OpenAPI.Operation\n if (!operation) throw new Error(`Operation ID \"${operationId}\" not found.`)\n const { method, path, responses = {} } = operation\n const fetchOptions = { method, baseUrl: getBaseUrl(specifications), ...initialOptions, ...options }\n const response = await fetch(path, fetchOptions)\n\n // --- Return the JSON response if successful.\n if (response.ok) return handleResponse(response, fetchOptions)\n\n // --- Throw an error if the response was not successful.\n const status = response.status.toString()\n if (status in responses\n && typeof responses[status] === 'object'\n && responses[status] !== null\n && 'description' in responses[status]\n && typeof responses[status].description === 'string')\n throw new Error(responses[status].description)\n\n // --- Throw a generic error if the response was not successful.\n throw new Error(response.statusText)\n }\n\n return new Proxy({}, {\n get(_, property: string) {\n if (property === 'fetch') return (route: string, options: RequestOptions) => fetch(route, ({ ...initialOptions, ...options }))\n return (options: Record<string, unknown>) => fetchByOperationId(property, options)\n },\n }) as unknown as Client\n}\n"],"names":["getOperationById","getBaseUrl","fetch","handleResponse"],"mappings":";;;;AA8DO,SAAS,aAAa,eAAoD,iBAAgC,IAAY;AAC3H,QAAM,iBAAiB,OAAO,iBAAkB,WAAW,SAAY;AACnE,SAAO,iBAAkB,aAAU,eAAe,UAAU;AAEjD,iBAAA,mBAAmB,aAAqB,SAA6B;AAClF,QAAI,CAAC,eAAsB,OAAA,IAAI,MAAM,oCAAoC;AACnE,UAAA,YAAYA,iBAAAA,iBAAiB,gBAAgB,WAAW;AAC9D,QAAI,CAAC,UAAW,OAAM,IAAI,MAAM,iBAAiB,WAAW,cAAc;AACpE,UAAA,EAAE,QAAQ,MAAM,YAAY,CAAA,EAAO,IAAA,WACnC,eAAe,EAAE,QAAQ,SAASC,4BAAW,cAAc,GAAG,GAAG,gBAAgB,GAAG,QAAA,GACpF,WAAW,MAAMC,MAAAA,MAAM,MAAM,YAAY;AAG/C,QAAI,SAAS,GAAW,QAAAC,eAAAA,eAAe,UAAU,YAAY;AAGvD,UAAA,SAAS,SAAS,OAAO,SAAS;AACxC,UAAI,UAAU,aACT,OAAO,UAAU,MAAM,KAAM,YAC7B,UAAU,MAAM,MAAM,QACtB,iBAAiB,UAAU,MAAM,KACjC,OAAO,UAAU,MAAM,EAAE,eAAgB,WACtC,IAAI,MAAM,UAAU,MAAM,EAAE,WAAW,IAGzC,IAAI,MAAM,SAAS,UAAU;AAAA,EAAA;AAG9B,SAAA,IAAI,MAAM,IAAI;AAAA,IACnB,IAAI,GAAG,UAAkB;AACvB,aAAI,aAAa,UAAgB,CAAC,OAAe,YAA4BD,MAAA,MAAM,OAAQ,EAAE,GAAG,gBAAgB,GAAG,SAAU,IACtH,CAAC,YAAqC,mBAAmB,UAAU,OAAO;AAAA,IAAA;AAAA,EACnF,CACD;AACH;;"}
|
package/dist/createClient.d.ts
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
import { Pretty, Override, MaybeLiteral } from '@unshared/types';
|
2
2
|
import { OpenAPI, OpenAPIV3, OpenAPIV2 as OpenAPIV2$1, OpenAPIV3_1 } from 'openapi-types';
|
3
|
-
import { O as OpenAPIV2, a as OpenAPIV3$1 } from './chunks/
|
3
|
+
import { O as OpenAPIV2, a as OpenAPIV3$1 } from './chunks/B2OrSen1.js';
|
4
4
|
import { R as RequestHooks } from './chunks/r8pYO6Hx.js';
|
5
|
-
import { c as RequestOptions
|
5
|
+
import { c as RequestOptions } from './chunks/DZp6zyqV.js';
|
6
6
|
import './chunks/8BFCFxqa.js';
|
7
7
|
|
8
8
|
type ClientBaseUrl<T> = MaybeLiteral<T extends OpenAPIV2$1.Document ? OpenAPIV2.ServerUrl<T> : T extends OpenAPIV3.Document ? OpenAPIV3$1.ServerUrl<T> : T extends OpenAPIV3_1.Document ? OpenAPIV3$1.ServerUrl<T> : string>;
|
@@ -20,7 +20,7 @@ type ClientOptions<T = any> = Override<RequestHooks & RequestOptions, {
|
|
20
20
|
*
|
21
21
|
* @example { 'Authorization': 'Bearer ...' }
|
22
22
|
*/
|
23
|
-
headers?: T extends OpenAPIV3.Document ? OpenAPIV3$1.ServerHeaders<T> :
|
23
|
+
headers?: T extends OpenAPIV3.Document ? OpenAPIV3$1.ServerHeaders<T> : never;
|
24
24
|
}>;
|
25
25
|
/**
|
26
26
|
* Create a new client instance for the given OpenAPI specification.
|
package/dist/createClient.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"createClient.js","sources":["../createClient.ts"],"sourcesContent":["import type { MaybeLiteral, Override, Pretty } from '@unshared/types'\nimport type { OpenAPI, OpenAPIV2 as V2, OpenAPIV3 as V3, OpenAPIV3_1 as V3_1 } from 'openapi-types'\nimport type { OpenAPIV2, OpenAPIV3 } from './openapi/index'\nimport type { RequestHooks } from './utils/handleResponse'\nimport type {
|
1
|
+
{"version":3,"file":"createClient.js","sources":["../createClient.ts"],"sourcesContent":["import type { MaybeLiteral, Override, Pretty } from '@unshared/types'\nimport type { OpenAPI, OpenAPIV2 as V2, OpenAPIV3 as V3, OpenAPIV3_1 as V3_1 } from 'openapi-types'\nimport type { OpenAPIV2, OpenAPIV3 } from './openapi/index'\nimport type { RequestHooks } from './utils/handleResponse'\nimport type { RequestMethod, RequestOptions } from './utils/parseRequest'\nimport { fetch } from './fetch'\nimport { getBaseUrl } from './openapi/getBaseUrl'\nimport { getOperationById } from './openapi/getOperationById'\nimport { handleResponse } from './utils/handleResponse'\n\ntype ClientBaseUrl<T> =\n MaybeLiteral<\n T extends V2.Document ? OpenAPIV2.ServerUrl<T>\n : T extends V3.Document ? OpenAPIV3.ServerUrl<T>\n : T extends V3_1.Document ? OpenAPIV3.ServerUrl<T>\n : string\n >\n\ntype ClientFetch<T> =\n T extends V2.Document ? <P extends OpenAPIV2.Route<T>>(name: P, options: OpenAPIV2.RequestInit<T, OpenAPIV2.OperationByRoute<T, P>>) => Promise<OpenAPIV2.Response<OpenAPIV2.OperationByRoute<T, P>>>\n : T extends V3.Document ? <P extends OpenAPIV2.Route<T>>(name: P, options: OpenAPIV3.RequestInit<T, OpenAPIV2.OperationByRoute<T, P>>) => Promise<OpenAPIV3.Response<OpenAPIV2.OperationByRoute<T, P>>>\n : T extends V3_1.Document ? <P extends OpenAPIV2.Route<T>>(name: P, options: OpenAPIV3.RequestInit<T, OpenAPIV2.OperationByRoute<T, P>>) => Promise<OpenAPIV3.Response<OpenAPIV2.OperationByRoute<T, P>>>\n : typeof globalThis.fetch\n\ntype ClientFetchOperation<T, U extends OpenAPIV2.OperationId<T>> =\n T extends V2.Document ? (options: OpenAPIV2.RequestInit<T, OpenAPIV2.OperationById<T, U>>) => Promise<OpenAPIV2.ResponseBody<OpenAPIV2.OperationById<T, U>>>\n : T extends V3.Document ? (options: OpenAPIV3.RequestInit<T, OpenAPIV2.OperationById<T, U>>) => Promise<OpenAPIV3.ResponseBody<OpenAPIV2.OperationById<T, U>>>\n : T extends V3_1.Document ? (options: OpenAPIV3.RequestInit<T, OpenAPIV2.OperationById<T, U>>) => Promise<OpenAPIV3.ResponseBody<OpenAPIV2.OperationById<T, U>>>\n : (options: RequestOptions) => Promise<Response>\n\nexport type Client<T = OpenAPI.Document> =\n Pretty<\n & { [K in OpenAPIV2.OperationId<T>]: ClientFetchOperation<T, K> }\n & { fetch: ClientFetch<T> }\n >\n\nexport type ClientOptions<T = any> = Override<RequestHooks & RequestOptions, {\n baseUrl?: ClientBaseUrl<T>\n\n /**\n * The headers to include in every request made by the client.\n *\n * @example { 'Authorization': 'Bearer ...' }\n */\n headers?: T extends V3.Document\n ? OpenAPIV3.ServerHeaders<T>\n : never\n}>\n\n/**\n * Create a new client instance for the given OpenAPI specification.\n *\n * @param document The OpenAPI specification document.\n * @param initialOptions The initial options to use for every request.\n * @returns The client instance.\n * @example\n * const client = createClient(document)\n * await client.fetch({ ... })\n */\n// @ts-expect-error: `ClientOptions` is not assignable to `ClientOptions<T>`.\nexport function createClient<T extends OpenAPI.Document>(document: Readonly<T>, initialOptions?: ClientOptions<T>): Client<T>\nexport function createClient<T extends OpenAPI.Document>(url: ClientBaseUrl<T>, initialOptions?: ClientOptions<T>): Client<T>\nexport function createClient(documentOrUrl: Readonly<OpenAPI.Document> | string, initialOptions: ClientOptions = {}): Client {\n const specifications = typeof documentOrUrl === 'string' ? undefined : documentOrUrl\n if (typeof documentOrUrl === 'string') initialOptions.baseUrl = documentOrUrl\n\n async function fetchByOperationId(operationId: string, options: ClientOptions<any>) {\n if (!specifications) throw new Error('No OpenAPI specification provided.')\n const operation = getOperationById(specifications, operationId) as { method: RequestMethod; path: string } & OpenAPI.Operation\n if (!operation) throw new Error(`Operation ID \"${operationId}\" not found.`)\n const { method, path, responses = {} } = operation\n const fetchOptions = { method, baseUrl: getBaseUrl(specifications), ...initialOptions, ...options }\n const response = await fetch(path, fetchOptions)\n\n // --- Return the JSON response if successful.\n if (response.ok) return handleResponse(response, fetchOptions)\n\n // --- Throw an error if the response was not successful.\n const status = response.status.toString()\n if (status in responses\n && typeof responses[status] === 'object'\n && responses[status] !== null\n && 'description' in responses[status]\n && typeof responses[status].description === 'string')\n throw new Error(responses[status].description)\n\n // --- Throw a generic error if the response was not successful.\n throw new Error(response.statusText)\n }\n\n return new Proxy({}, {\n get(_, property: string) {\n if (property === 'fetch') return (route: string, options: RequestOptions) => fetch(route, ({ ...initialOptions, ...options }))\n return (options: Record<string, unknown>) => fetchByOperationId(property, options)\n },\n }) as unknown as Client\n}\n"],"names":[],"mappings":";;;;;AA8DO,SAAS,aAAa,eAAoD,iBAAgC,IAAY;AAC3H,QAAM,iBAAiB,OAAO,iBAAkB,WAAW,SAAY;AACnE,SAAO,iBAAkB,aAAU,eAAe,UAAU;AAEjD,iBAAA,mBAAmB,aAAqB,SAA6B;AAClF,QAAI,CAAC,eAAsB,OAAA,IAAI,MAAM,oCAAoC;AACnE,UAAA,YAAY,iBAAiB,gBAAgB,WAAW;AAC9D,QAAI,CAAC,UAAW,OAAM,IAAI,MAAM,iBAAiB,WAAW,cAAc;AACpE,UAAA,EAAE,QAAQ,MAAM,YAAY,CAAA,EAAO,IAAA,WACnC,eAAe,EAAE,QAAQ,SAAS,WAAW,cAAc,GAAG,GAAG,gBAAgB,GAAG,QAAA,GACpF,WAAW,MAAM,MAAM,MAAM,YAAY;AAG/C,QAAI,SAAS,GAAW,QAAA,eAAe,UAAU,YAAY;AAGvD,UAAA,SAAS,SAAS,OAAO,SAAS;AACxC,UAAI,UAAU,aACT,OAAO,UAAU,MAAM,KAAM,YAC7B,UAAU,MAAM,MAAM,QACtB,iBAAiB,UAAU,MAAM,KACjC,OAAO,UAAU,MAAM,EAAE,eAAgB,WACtC,IAAI,MAAM,UAAU,MAAM,EAAE,WAAW,IAGzC,IAAI,MAAM,SAAS,UAAU;AAAA,EAAA;AAG9B,SAAA,IAAI,MAAM,IAAI;AAAA,IACnB,IAAI,GAAG,UAAkB;AACvB,aAAI,aAAa,UAAgB,CAAC,OAAe,YAA4B,MAAM,OAAQ,EAAE,GAAG,gBAAgB,GAAG,SAAU,IACtH,CAAC,YAAqC,mBAAmB,UAAU,OAAO;AAAA,IAAA;AAAA,EACnF,CACD;AACH;"}
|
package/dist/index.d.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
export { Client, ClientOptions, createClient } from './createClient.js';
|
2
2
|
export { fetch } from './fetch.js';
|
3
3
|
export { OpenAPIReference, OpenAPIReferenceDecoded, OpenAPIReferencePath, OpenAPIReferenceResolved, OpenAPIResolved, getBaseUrl, getOperationById, getOperationByRoute, isReferenceObject, resolveDocument, resolveReference } from './openapi.js';
|
4
|
-
export { O as OpenAPIV2, a as OpenAPIV3 } from './chunks/
|
4
|
+
export { O as OpenAPIV2, a as OpenAPIV3 } from './chunks/B2OrSen1.js';
|
5
5
|
export { H as HttpHeader } from './chunks/8BFCFxqa.js';
|
6
6
|
export { H as HttpMethod, b as RequestBody, d as RequestContext, a as RequestHeaders, R as RequestMethod, c as RequestOptions, e as SearchArrayFormat, S as SearchParamsObject, T as ToSearchParamsOptions, p as parseRequest, t as toSearchParams } from './chunks/DZp6zyqV.js';
|
7
7
|
export { R as RequestHooks, h as handleResponse } from './chunks/r8pYO6Hx.js';
|
package/dist/openapi.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import { OpenAPI, OpenAPIV2 as OpenAPIV2$1, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types';
|
2
|
-
import { O as OpenAPIV2 } from './chunks/
|
3
|
-
export { a as OpenAPIV3 } from './chunks/
|
2
|
+
import { O as OpenAPIV2 } from './chunks/B2OrSen1.js';
|
3
|
+
export { a as OpenAPIV3 } from './chunks/B2OrSen1.js';
|
4
4
|
import { StringReplace, WriteableDeep, StringJoin, Substract } from '@unshared/types';
|
5
5
|
import './chunks/8BFCFxqa.js';
|
6
6
|
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@unshared/client",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.2.
|
4
|
+
"version": "0.2.3",
|
5
5
|
"license": "MIT",
|
6
6
|
"sideEffects": false,
|
7
7
|
"author": "Stanley Horwood <stanley@hsjm.io>",
|
@@ -48,12 +48,12 @@
|
|
48
48
|
"LICENSE.md"
|
49
49
|
],
|
50
50
|
"dependencies": {
|
51
|
-
"@unshared/functions": "0.2.
|
52
|
-
"@unshared/types": "0.2.
|
51
|
+
"@unshared/functions": "0.2.3",
|
52
|
+
"@unshared/types": "0.2.3",
|
53
53
|
"openapi-types": "12.1.3"
|
54
54
|
},
|
55
55
|
"devDependencies": {
|
56
|
-
"@unshared/scripts": "0.2.
|
56
|
+
"@unshared/scripts": "0.2.3"
|
57
57
|
},
|
58
58
|
"scripts": {
|
59
59
|
"build:httpMethods": "tsx ./scripts/buildHttpMethods.ts",
|