@unshared/client 0.2.3 → 0.3.1
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/HttpHeaders.cjs +4 -0
- package/dist/HttpHeaders.cjs.map +1 -0
- package/dist/{chunks/8BFCFxqa.d.ts → HttpHeaders.d.ts} +1 -1
- package/dist/HttpHeaders.js +5 -0
- package/dist/HttpHeaders.js.map +1 -0
- package/dist/HttpMethods.cjs +4 -0
- package/dist/HttpMethods.cjs.map +1 -0
- package/dist/{chunks/DZp6zyqV.d.ts → HttpMethods.d.ts} +1 -98
- package/dist/HttpMethods.js +5 -0
- package/dist/HttpMethods.js.map +1 -0
- package/dist/HttpStatusCodes.cjs +4 -0
- package/dist/HttpStatusCodes.cjs.map +1 -0
- package/dist/HttpStatusCodes.d.ts +615 -0
- package/dist/HttpStatusCodes.js +5 -0
- package/dist/HttpStatusCodes.js.map +1 -0
- package/dist/chunks/0ZzUT3m_.js +46 -0
- package/dist/chunks/0ZzUT3m_.js.map +1 -0
- package/dist/chunks/BUeqbyph.js +114 -0
- package/dist/chunks/BUeqbyph.js.map +1 -0
- package/dist/chunks/{D-WqCFul.js → Biic1J5b.js} +11 -9
- package/dist/chunks/Biic1J5b.js.map +1 -0
- package/dist/chunks/CO11DuYE.d.ts +144 -0
- package/dist/chunks/CYmaYL5B.cjs +45 -0
- package/dist/chunks/CYmaYL5B.cjs.map +1 -0
- package/dist/chunks/Cayg8606.cjs +112 -0
- package/dist/chunks/Cayg8606.cjs.map +1 -0
- package/dist/chunks/{xRZPkxch.cjs → CtW2aMuA.cjs} +11 -9
- package/dist/chunks/CtW2aMuA.cjs.map +1 -0
- package/dist/chunks/DJJsADWD.js +9 -0
- package/dist/chunks/DJJsADWD.js.map +1 -0
- package/dist/chunks/DOZHjge0.d.ts +224 -0
- package/dist/chunks/Du56lBvc.js +75 -0
- package/dist/chunks/Du56lBvc.js.map +1 -0
- package/dist/chunks/Du_W5H6e.cjs +8 -0
- package/dist/chunks/Du_W5H6e.cjs.map +1 -0
- package/dist/chunks/VkINJoq7.cjs +74 -0
- package/dist/chunks/VkINJoq7.cjs.map +1 -0
- package/dist/createClient.cjs +82 -18
- package/dist/createClient.cjs.map +1 -1
- package/dist/createClient.d.ts +109 -29
- package/dist/createClient.js +85 -20
- package/dist/createClient.js.map +1 -1
- package/dist/createService.cjs +18 -0
- package/dist/createService.cjs.map +1 -0
- package/dist/createService.d.ts +38 -0
- package/dist/createService.js +20 -0
- package/dist/createService.js.map +1 -0
- package/dist/index.cjs +12 -26
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +9 -625
- package/dist/index.js +13 -27
- package/dist/index.js.map +1 -1
- package/dist/openapi.cjs +3 -15
- package/dist/openapi.cjs.map +1 -1
- package/dist/openapi.d.ts +7 -35
- package/dist/openapi.js +3 -15
- package/dist/openapi.js.map +1 -1
- package/dist/utils.cjs +13 -11
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.ts +57 -27
- package/dist/utils.js +12 -9
- package/dist/utils.js.map +1 -1
- package/dist/websocket.cjs +7 -0
- package/dist/websocket.cjs.map +1 -0
- package/dist/websocket.d.ts +148 -0
- package/dist/websocket.js +8 -0
- package/dist/websocket.js.map +1 -0
- package/package.json +28 -8
- package/dist/chunks/B2OrSen1.d.ts +0 -185
- package/dist/chunks/B5hc73Po.js +0 -96
- package/dist/chunks/B5hc73Po.js.map +0 -1
- package/dist/chunks/CKfJvIQ8.js +0 -50
- package/dist/chunks/CKfJvIQ8.js.map +0 -1
- package/dist/chunks/Cx8m1YzL.cjs +0 -49
- package/dist/chunks/Cx8m1YzL.cjs.map +0 -1
- package/dist/chunks/D-WqCFul.js.map +0 -1
- package/dist/chunks/D8Tsm7xC.cjs +0 -95
- package/dist/chunks/D8Tsm7xC.cjs.map +0 -1
- package/dist/chunks/r8pYO6Hx.d.ts +0 -38
- package/dist/chunks/xRZPkxch.cjs.map +0 -1
- package/dist/fetch.cjs +0 -8
- package/dist/fetch.cjs.map +0 -1
- package/dist/fetch.d.ts +0 -16
- package/dist/fetch.js +0 -9
- package/dist/fetch.js.map +0 -1
package/dist/openapi.cjs
CHANGED
@@ -1,16 +1,5 @@
|
|
1
1
|
"use strict";
|
2
|
-
var
|
3
|
-
function getOperationByRoute(specification, name) {
|
4
|
-
if (!specification || typeof specification != "object" || specification === null || !("paths" in specification) || typeof specification.paths != "object" || specification.paths === null)
|
5
|
-
throw new Error("Missing paths object in the OpenAPI specification.");
|
6
|
-
const match = /^(get|post|put|patch|delete|head|options) (\/.+)$/i.exec(name);
|
7
|
-
if (!match) throw new Error("Could not resolve the path and method from the route name.");
|
8
|
-
const [, routeMethod, routePath] = match, method = routeMethod.toLowerCase(), path = specification.paths[routePath];
|
9
|
-
if (!path) throw new Error(`Route "${name}" not found in specification.`);
|
10
|
-
if (typeof path != "object" || path === null) throw new Error("Invalid path object in the OpenAPI specification.");
|
11
|
-
if (!(method in path)) throw new Error(`Method "${method}" not found in path "${routePath}".`);
|
12
|
-
return { ...path[method], method, path: routePath };
|
13
|
-
}
|
2
|
+
var resolveOperation = require("./chunks/CtW2aMuA.cjs");
|
14
3
|
function isReferenceObject(value) {
|
15
4
|
return typeof value == "object" && value !== null && "$ref" in value && typeof value.$ref == "string";
|
16
5
|
}
|
@@ -38,9 +27,8 @@ function resolveDocument(value, document = value) {
|
|
38
27
|
}
|
39
28
|
});
|
40
29
|
}
|
41
|
-
exports.
|
42
|
-
exports.
|
43
|
-
exports.getOperationByRoute = getOperationByRoute;
|
30
|
+
exports.getServerUrl = resolveOperation.getServerUrl;
|
31
|
+
exports.resolveOperation = resolveOperation.resolveOperation;
|
44
32
|
exports.isReferenceObject = isReferenceObject;
|
45
33
|
exports.resolveDocument = resolveDocument;
|
46
34
|
exports.resolveReference = resolveReference;
|
package/dist/openapi.cjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"openapi.cjs","sources":["../openapi/getOperationByRoute.ts","../openapi/isReferenceObject.ts","../openapi/resolveReference.ts","../openapi/resolveDocument.ts"],"sourcesContent":["import type { OpenAPI } from 'openapi-types'\nimport type { OpenAPIV2 } from './OpenApiV2'\n\n/**\n * Given an OpenAPI specification, find a route by its name.\n *\n * @param specification The OpenAPI specification.\n * @param name The name of the route to resolve.\n * @returns The resolved route.\n * @example getOperationByRoute(specification, 'GET /users') // { method: 'get', path: '/users', ... }\n */\nexport function getOperationByRoute<\n T extends object,\n U extends OpenAPIV2.Route<T>,\n>(\n specification: Readonly<T>,\n name: U,\n): OpenAPIV2.OperationByRoute<T, U> {\n\n // --- Assert the specification has a paths object.\n if (!specification\n || typeof specification !== 'object'\n || specification === null\n || 'paths' in specification === false\n || typeof specification.paths !== 'object'\n || specification.paths === null)\n throw new Error('Missing paths object in the OpenAPI specification.')\n\n // --- Extract the path and method from the name.\n const match = /^(get|post|put|patch|delete|head|options) (\\/.+)$/i.exec(name)\n if (!match) throw new Error('Could not resolve the path and method from the route name.')\n const [, routeMethod, routePath] = match\n const method = routeMethod.toLowerCase()\n\n // --- Search for the route in the specification's paths.\n const paths = specification.paths as Record<string, OpenAPI.Operation>\n const path = paths[routePath]\n if (!path) throw new Error(`Route \"${name}\" not found in specification.`)\n if (typeof path !== 'object' || path === null) throw new Error('Invalid path object in the OpenAPI specification.')\n if (method in path === false) throw new Error(`Method \"${method}\" not found in path \"${routePath}\".`)\n const operation = path[method as keyof typeof path] as OpenAPI.Operation\n return { ...operation, method, path: routePath } as OpenAPIV2.OperationByRoute<T, U>\n}\n","import type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\nexport type OpenAPIReference =\n | OpenAPIV2.ReferenceObject\n | OpenAPIV3.ReferenceObject\n | OpenAPIV3_1.ReferenceObject\n\n/**\n * Check if a value is an {@linkcode OpenAPIReference}.\n *\n * @param value The value to check.\n * @returns `true` if the value is a reference object.\n * @example isReferenceObject({ $ref: '#/components/schemas/MySchema' }) // true\n */\nexport function isReferenceObject<T extends OpenAPIReference>(value: unknown): value is T {\n return typeof value === 'object'\n && value !== null\n && '$ref' in value\n && typeof value.$ref === 'string'\n}\n","import type { StringJoin, StringReplace, WriteableDeep } from '@unshared/types'\nimport type { OpenAPIReference } from './isReferenceObject'\nimport { isReferenceObject } from './isReferenceObject'\n\n/**\n * Decode an OpenAPI reference path by replacing the encoded characters with\n * their original values. This function will replace `~0` with `~` and `~1`\n * with `/`.\n *\n * @example DecodeReference<'#/foo~1bar~0baz'> // '#/foo/bar~baz'\n */\nexport type OpenAPIReferenceDecoded<T extends string> =\n StringReplace<StringReplace<T, '~0', '~'>, '~1', '/'>\n\n/**\n * Extract the parts of a reference path as a tuple.\n *\n * @example OpenAPIV3ReferencePath<'#/paths/~1users~1{username}'> // ['paths', '/users/{username}']\n */\nexport type OpenAPIReferencePath<T extends string> =\n T extends `#/${infer P}/${infer Rest}` ? [P, ...OpenAPIReferencePath<Rest>]\n : T extends `#/${infer P}` ? [P]\n : T extends `${infer P}/${infer Rest}` ? [P, ...OpenAPIReferencePath<Rest>]\n : T extends `${infer P}` ? [P]\n : []\n\n/**\n * Resolve a type to the type it references. If the source is not a reference,\n * the source will be returned.\n *\n * @template T The type to resolve.\n * @returns The result type.\n * @example Resolved<{ $ref: '#/info' }, { info: { title: string } }> // { title: string }\n */\nexport type OpenAPIReferenceResolved<\n T extends OpenAPIReference,\n D extends object,\n> =\n D extends object\n ? T extends { $ref: infer R extends string }\n\n // --- Match last part of the reference.\n ? OpenAPIReferencePath<R> extends [infer P extends string]\n ? OpenAPIReferenceDecoded<P> extends keyof D\n ? D[OpenAPIReferenceDecoded<P>] extends object\n ? WriteableDeep<Omit<D[OpenAPIReferenceDecoded<P>], keyof T>>\n : never\n : never\n\n // --- Match middle part of the reference.\n : OpenAPIReferencePath<R> extends [infer P extends string, ...infer Rest extends string[]]\n ? OpenAPIReferenceDecoded<P> extends keyof D\n ? D[OpenAPIReferenceDecoded<P>] extends object\n ? OpenAPIReferenceResolved<{ $ref: StringJoin<Rest, '/'> }, D[OpenAPIReferenceDecoded<P>]>\n : never\n : never\n : never\n : never\n : never\n\n/**\n * Resolve an OpenAPI `ReferenceObject` to the component it references. If the\n * source is not a reference, the source will be returned.\n *\n * @private\n * @param reference The reference object to resolve.\n * @param document The OpenAPI document to resolve the reference from.\n * @returns The result component.\n * @example resolveReference({ $ref: '#/components/schemas/User' }, document)\n */\nexport function resolveReference<\n T extends OpenAPIReference,\n D extends object,\n>(reference: Readonly<T>, document: Readonly<D>): OpenAPIReferenceResolved<T, D> {\n\n // --- Return the source if it is not a reference.\n if (!isReferenceObject(reference))\n throw new TypeError('Expected value to be an OpenAPI reference object.')\n\n // --- Assert that the parameters are valid.\n if (typeof document !== 'object' || document === null)\n throw new TypeError('Expected OpenAPI specification to be an object.')\n\n // --- Resolve the component with it's reference path.\n const referenceParts = reference.$ref.replace(/^#\\//, '').split('/')\n let result = document\n for (const part of referenceParts) {\n if (result === undefined) break\n if (typeof result !== 'object' || result === null) break\n const key = part.replaceAll('~1', '/').replaceAll('~0', '~')\n // @ts-expect-error: assume the part is a key of the object.\n result = result[key] as unknown\n }\n\n // --- Throw an error if the component could not be result.\n if (result === undefined)\n throw new Error(`Could not resolve OpenAPI component: ${reference.$ref}`)\n\n // --- Return the result component.\n return result as OpenAPIReferenceResolved<T, D>\n}\n","import type { ObjectLike, Substract } from '@unshared/types'\nimport type { OpenAPIReference } from './isReferenceObject'\nimport type { OpenAPIReferenceResolved } from './resolveReference'\nimport { isReferenceObject } from './isReferenceObject'\nimport { resolveReference as resolveReference } from './resolveReference'\n\n/**\n * Resolve a type to the type it references. If the source is not a reference,\n * the source will be returned.\n *\n * @template T The type to resolve.\n * @returns The resolved type.\n * @example\n * type Resolved = OpenAPIResolved<{\n * ...\n * paths: {\n * '/users': {\n * get: { $ref: '#/components/routes/getUsers' }\n * }\n * }\n * }>\n */\nexport type OpenAPIResolved<T, D = T, N extends number = 8> =\n N extends 0 ? T\n : T extends OpenAPIReference\n ? D extends object ? OpenAPIResolved<OpenAPIReferenceResolved<T, D>, D, Substract<N, 1>> : never\n : T extends object ? { -readonly [K in keyof T]: OpenAPIResolved<T[K], D, N> } : T\n\n/**\n * Recursively resolve all references in an OpenAPI specification. This function\n * will return a `Proxy` object that will resolve references on the fly.\n *\n * @param value The OpenAPI specification.\n * @returns The resolved OpenAPI specification.\n * @example\n * const resolved = resolveReferences({\n * ...\n * paths: {\n * '/users': {\n * get: { $ref: '#/components/routes/getUsers' },\n * },\n * },\n * })\n */\nexport function resolveDocument<T extends object>(value: Readonly<T>): OpenAPIResolved<T>\nexport function resolveDocument<T extends object, D>(value: Readonly<T>, document: Readonly<D>): OpenAPIResolved<T, D>\nexport function resolveDocument(value: Readonly<ObjectLike>, document = value): unknown {\n return new Proxy(value, {\n get(target, property: string) {\n let value = target[property]\n\n // --- Abort if no document is provided.\n if (!document) return value\n\n // --- Resolve the reference if it is a reference object.\n if (isReferenceObject(value))\n value = resolveReference(value, document)\n\n // --- Recursively resolve references in objects.\n if (typeof value === 'object' && value !== null)\n return resolveDocument(value as ObjectLike, document)\n\n // --- Return the value as is.\n return value\n },\n })\n}\n"],"names":["value"],"mappings":";;AAWgB,SAAA,oBAId,eACA,MACkC;AAGlC,MAAI,CAAC,iBACA,OAAO,iBAAkB,YACzB,kBAAkB,QAClB,EAAW,WAAA,kBACX,OAAO,cAAc,SAAU,YAC/B,cAAc,UAAU;AACrB,UAAA,IAAI,MAAM,oDAAoD;AAGhE,QAAA,QAAQ,qDAAqD,KAAK,IAAI;AAC5E,MAAI,CAAC,MAAa,OAAA,IAAI,MAAM,4DAA4D;AACxF,QAAM,GAAG,aAAa,SAAS,IAAI,OAC7B,SAAS,YAAY,YAAY,GAIjC,OADQ,cAAc,MACT,SAAS;AAC5B,MAAI,CAAC,KAAM,OAAM,IAAI,MAAM,UAAU,IAAI,+BAA+B;AACpE,MAAA,OAAO,QAAS,YAAY,SAAS,KAAY,OAAA,IAAI,MAAM,mDAAmD;AAC9G,MAAA,EAAA,UAAU,MAAsB,OAAA,IAAI,MAAM,WAAW,MAAM,wBAAwB,SAAS,IAAI;AAEpG,SAAO,EAAE,GADS,KAAK,MAA2B,GAC3B,QAAQ,MAAM,UAAU;AACjD;AC5BO,SAAS,kBAA8C,OAA4B;AACjF,SAAA,OAAO,SAAU,YACnB,UAAU,QACV,UAAU,SACV,OAAO,MAAM,QAAS;AAC7B;ACmDgB,SAAA,iBAGd,WAAwB,UAAuD;AAG3E,MAAA,CAAC,kBAAkB,SAAS;AACxB,UAAA,IAAI,UAAU,mDAAmD;AAGrE,MAAA,OAAO,YAAa,YAAY,aAAa;AACzC,UAAA,IAAI,UAAU,iDAAiD;AAGjE,QAAA,iBAAiB,UAAU,KAAK,QAAQ,QAAQ,EAAE,EAAE,MAAM,GAAG;AACnE,MAAI,SAAS;AACb,aAAW,QAAQ,gBAAgB;AAEjC,QADI,WAAW,UACX,OAAO,UAAW,YAAY,WAAW,KAAM;AAC7C,UAAA,MAAM,KAAK,WAAW,MAAM,GAAG,EAAE,WAAW,MAAM,GAAG;AAE3D,aAAS,OAAO,GAAG;AAAA,EAAA;AAIrB,MAAI,WAAW;AACb,UAAM,IAAI,MAAM,wCAAwC,UAAU,IAAI,EAAE;AAGnE,SAAA;AACT;ACtDgB,SAAA,gBAAgB,OAA6B,WAAW,OAAgB;AAC/E,SAAA,IAAI,MAAM,OAAO;AAAA,IACtB,IAAI,QAAQ,UAAkB;AACxBA,UAAAA,SAAQ,OAAO,QAAQ;AAG3B,aAAK,YAGD,kBAAkBA,MAAK,MACzBA,SAAQ,iBAAiBA,QAAO,QAAQ,IAGtC,OAAOA,UAAU,YAAYA,WAAU,OAClC,gBAAgBA,QAAqB,QAAQ,IAG/CA,UAXeA;AAAAA,IAAA;AAAA,EAYxB,CACD;AACH;;;;;;;"}
|
1
|
+
{"version":3,"file":"openapi.cjs","sources":["../openapi/isReferenceObject.ts","../openapi/resolveReference.ts","../openapi/resolveDocument.ts"],"sourcesContent":["import type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\nexport type OpenAPIReference =\n | OpenAPIV2.ReferenceObject\n | OpenAPIV3.ReferenceObject\n | OpenAPIV3_1.ReferenceObject\n\n/**\n * Check if a value is an {@linkcode OpenAPIReference}.\n *\n * @param value The value to check.\n * @returns `true` if the value is a reference object.\n * @example isReferenceObject({ $ref: '#/components/schemas/MySchema' }) // true\n */\nexport function isReferenceObject<T extends OpenAPIReference>(value: unknown): value is T {\n return typeof value === 'object'\n && value !== null\n && '$ref' in value\n && typeof value.$ref === 'string'\n}\n","import type { StringJoin, StringReplace, WriteableDeep } from '@unshared/types'\nimport type { OpenAPIReference } from './isReferenceObject'\nimport { isReferenceObject } from './isReferenceObject'\n\n/**\n * Decode an OpenAPI reference path by replacing the encoded characters with\n * their original values. This function will replace `~0` with `~` and `~1`\n * with `/`.\n *\n * @example DecodeReference<'#/foo~1bar~0baz'> // '#/foo/bar~baz'\n */\nexport type OpenAPIReferenceDecoded<T extends string> =\n StringReplace<StringReplace<T, '~0', '~'>, '~1', '/'>\n\n/**\n * Extract the parts of a reference path as a tuple.\n *\n * @example OpenAPIV3ReferencePath<'#/paths/~1users~1{username}'> // ['paths', '/users/{username}']\n */\nexport type OpenAPIReferencePath<T extends string> =\n T extends `#/${infer P}/${infer Rest}` ? [P, ...OpenAPIReferencePath<Rest>]\n : T extends `#/${infer P}` ? [P]\n : T extends `${infer P}/${infer Rest}` ? [P, ...OpenAPIReferencePath<Rest>]\n : T extends `${infer P}` ? [P]\n : []\n\n/**\n * Resolve a type to the type it references. If the source is not a reference,\n * the source will be returned.\n *\n * @template T The type to resolve.\n * @returns The result type.\n * @example Resolved<{ $ref: '#/info' }, { info: { title: string } }> // { title: string }\n */\nexport type OpenAPIReferenceResolved<\n T extends OpenAPIReference,\n D extends object,\n> =\n D extends object\n ? T extends { $ref: infer R extends string }\n\n // --- Match last part of the reference.\n ? OpenAPIReferencePath<R> extends [infer P extends string]\n ? OpenAPIReferenceDecoded<P> extends keyof D\n ? D[OpenAPIReferenceDecoded<P>] extends object\n ? WriteableDeep<Omit<D[OpenAPIReferenceDecoded<P>], keyof T>>\n : never\n : never\n\n // --- Match middle part of the reference.\n : OpenAPIReferencePath<R> extends [infer P extends string, ...infer Rest extends string[]]\n ? OpenAPIReferenceDecoded<P> extends keyof D\n ? D[OpenAPIReferenceDecoded<P>] extends object\n ? OpenAPIReferenceResolved<{ $ref: StringJoin<Rest, '/'> }, D[OpenAPIReferenceDecoded<P>]>\n : never\n : never\n : never\n : never\n : never\n\n/**\n * Resolve an OpenAPI `ReferenceObject` to the component it references. If the\n * source is not a reference, the source will be returned.\n *\n * @private\n * @param reference The reference object to resolve.\n * @param document The OpenAPI document to resolve the reference from.\n * @returns The result component.\n * @example resolveReference({ $ref: '#/components/schemas/User' }, document)\n */\nexport function resolveReference<\n T extends OpenAPIReference,\n D extends object,\n>(reference: Readonly<T>, document: Readonly<D>): OpenAPIReferenceResolved<T, D> {\n\n // --- Return the source if it is not a reference.\n if (!isReferenceObject(reference))\n throw new TypeError('Expected value to be an OpenAPI reference object.')\n\n // --- Assert that the parameters are valid.\n if (typeof document !== 'object' || document === null)\n throw new TypeError('Expected OpenAPI specification to be an object.')\n\n // --- Resolve the component with it's reference path.\n const referenceParts = reference.$ref.replace(/^#\\//, '').split('/')\n let result = document\n for (const part of referenceParts) {\n if (result === undefined) break\n if (typeof result !== 'object' || result === null) break\n const key = part.replaceAll('~1', '/').replaceAll('~0', '~')\n // @ts-expect-error: assume the part is a key of the object.\n result = result[key] as unknown\n }\n\n // --- Throw an error if the component could not be result.\n if (result === undefined)\n throw new Error(`Could not resolve OpenAPI component: ${reference.$ref}`)\n\n // --- Return the result component.\n return result as OpenAPIReferenceResolved<T, D>\n}\n","import type { ObjectLike, Substract } from '@unshared/types'\nimport type { OpenAPIReference } from './isReferenceObject'\nimport type { OpenAPIReferenceResolved } from './resolveReference'\nimport { isReferenceObject } from './isReferenceObject'\nimport { resolveReference as resolveReference } from './resolveReference'\n\n/**\n * Resolve a type to the type it references. If the source is not a reference,\n * the source will be returned.\n *\n * @template T The type to resolve.\n * @returns The resolved type.\n * @example\n * type Resolved = OpenAPIResolved<{\n * ...\n * paths: {\n * '/users': {\n * get: { $ref: '#/components/routes/getUsers' }\n * }\n * }\n * }>\n */\nexport type OpenAPIResolved<T, D = T, N extends number = 8> =\n N extends 0 ? T\n : T extends OpenAPIReference\n ? D extends object ? OpenAPIResolved<OpenAPIReferenceResolved<T, D>, D, Substract<N, 1>> : never\n : T extends object ? { -readonly [K in keyof T]: OpenAPIResolved<T[K], D, N> } : T\n\n/**\n * Recursively resolve all references in an OpenAPI specification. This function\n * will return a `Proxy` object that will resolve references on the fly.\n *\n * @param value The OpenAPI specification.\n * @returns The resolved OpenAPI specification.\n * @example\n * const resolved = resolveReferences({\n * ...\n * paths: {\n * '/users': {\n * get: { $ref: '#/components/routes/getUsers' },\n * },\n * },\n * })\n */\nexport function resolveDocument<T extends object>(value: Readonly<T>): OpenAPIResolved<T>\nexport function resolveDocument<T extends object, D>(value: Readonly<T>, document: Readonly<D>): OpenAPIResolved<T, D>\nexport function resolveDocument(value: Readonly<ObjectLike>, document = value): unknown {\n return new Proxy(value, {\n get(target, property: string) {\n let value = target[property]\n\n // --- Abort if no document is provided.\n if (!document) return value\n\n // --- Resolve the reference if it is a reference object.\n if (isReferenceObject(value))\n value = resolveReference(value, document)\n\n // --- Recursively resolve references in objects.\n if (typeof value === 'object' && value !== null)\n return resolveDocument(value as ObjectLike, document)\n\n // --- Return the value as is.\n return value\n },\n })\n}\n"],"names":["value"],"mappings":";;AAcO,SAAS,kBAA8C,OAA4B;AACjF,SAAA,OAAO,SAAU,YACnB,UAAU,QACV,UAAU,SACV,OAAO,MAAM,QAAS;AAC7B;ACmDgB,SAAA,iBAGd,WAAwB,UAAuD;AAG3E,MAAA,CAAC,kBAAkB,SAAS;AACxB,UAAA,IAAI,UAAU,mDAAmD;AAGrE,MAAA,OAAO,YAAa,YAAY,aAAa;AACzC,UAAA,IAAI,UAAU,iDAAiD;AAGjE,QAAA,iBAAiB,UAAU,KAAK,QAAQ,QAAQ,EAAE,EAAE,MAAM,GAAG;AACnE,MAAI,SAAS;AACb,aAAW,QAAQ,gBAAgB;AAEjC,QADI,WAAW,UACX,OAAO,UAAW,YAAY,WAAW,KAAM;AAC7C,UAAA,MAAM,KAAK,WAAW,MAAM,GAAG,EAAE,WAAW,MAAM,GAAG;AAE3D,aAAS,OAAO,GAAG;AAAA,EAAA;AAIrB,MAAI,WAAW;AACb,UAAM,IAAI,MAAM,wCAAwC,UAAU,IAAI,EAAE;AAGnE,SAAA;AACT;ACtDgB,SAAA,gBAAgB,OAA6B,WAAW,OAAgB;AAC/E,SAAA,IAAI,MAAM,OAAO;AAAA,IACtB,IAAI,QAAQ,UAAkB;AACxBA,UAAAA,SAAQ,OAAO,QAAQ;AAG3B,aAAK,YAGD,kBAAkBA,MAAK,MACzBA,SAAQ,iBAAiBA,QAAO,QAAQ,IAGtC,OAAOA,UAAU,YAAYA,WAAU,OAClC,gBAAgBA,QAAqB,QAAQ,IAG/CA,UAXeA;AAAAA,IAAA;AAAA,EAYxB,CACD;AACH;;;;;;"}
|
package/dist/openapi.d.ts
CHANGED
@@ -1,39 +1,11 @@
|
|
1
|
-
|
2
|
-
import {
|
3
|
-
export { a as OpenAPIV3 } from './chunks/B2OrSen1.js';
|
1
|
+
export { O as OpenAPILike, a as OpenAPIOptionsMap, h as OpenAPIV2, j as OpenAPIV2Like, f as OpenAPIV3, k as OpenAPIV3Like, i as Operation, c as OperationById, n as OperationByRoute, b as OperationId, d as OperationOptions, l as OperationResponse, e as OperationResult, m as OperationRoute, S as ServerUrl, g as getServerUrl, r as resolveOperation } from './chunks/DOZHjge0.js';
|
2
|
+
import { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types';
|
4
3
|
import { StringReplace, WriteableDeep, StringJoin, Substract } from '@unshared/types';
|
5
|
-
import './chunks/
|
4
|
+
import './chunks/CO11DuYE.js';
|
5
|
+
import './HttpHeaders.js';
|
6
|
+
import './HttpMethods.js';
|
6
7
|
|
7
|
-
|
8
|
-
* Given an OpenAPI specification, get the first base URL.
|
9
|
-
*
|
10
|
-
* @param specification The OpenAPI specification.
|
11
|
-
* @returns The first base URL.
|
12
|
-
* @example getBaseUrl(specification) // 'https://api.example.com/v1'
|
13
|
-
*/
|
14
|
-
declare function getBaseUrl(specification: OpenAPI.Document): string;
|
15
|
-
|
16
|
-
/**
|
17
|
-
* Given an OpenAPI specification, find an operation by its operationId.
|
18
|
-
*
|
19
|
-
* @param specification The OpenAPI specification.
|
20
|
-
* @param operationId The operationId of the operation to resolve.
|
21
|
-
* @returns The resolved operation.
|
22
|
-
* @example openapiGetOperation(specification, 'getUser') // { method: 'get', path: '/users/{username}', ... }
|
23
|
-
*/
|
24
|
-
declare function getOperationById<T, U extends OpenAPIV2.OperationId<T>>(specification: T, operationId: U): OpenAPIV2.OperationById<T, U>;
|
25
|
-
|
26
|
-
/**
|
27
|
-
* Given an OpenAPI specification, find a route by its name.
|
28
|
-
*
|
29
|
-
* @param specification The OpenAPI specification.
|
30
|
-
* @param name The name of the route to resolve.
|
31
|
-
* @returns The resolved route.
|
32
|
-
* @example getOperationByRoute(specification, 'GET /users') // { method: 'get', path: '/users', ... }
|
33
|
-
*/
|
34
|
-
declare function getOperationByRoute<T extends object, U extends OpenAPIV2.Route<T>>(specification: Readonly<T>, name: U): OpenAPIV2.OperationByRoute<T, U>;
|
35
|
-
|
36
|
-
type OpenAPIReference = OpenAPIV2$1.ReferenceObject | OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject;
|
8
|
+
type OpenAPIReference = OpenAPIV2.ReferenceObject | OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject;
|
37
9
|
/**
|
38
10
|
* Check if a value is an {@linkcode OpenAPIReference}.
|
39
11
|
*
|
@@ -120,4 +92,4 @@ type OpenAPIResolved<T, D = T, N extends number = 8> = N extends 0 ? T : T exten
|
|
120
92
|
declare function resolveDocument<T extends object>(value: Readonly<T>): OpenAPIResolved<T>;
|
121
93
|
declare function resolveDocument<T extends object, D>(value: Readonly<T>, document: Readonly<D>): OpenAPIResolved<T, D>;
|
122
94
|
|
123
|
-
export { type OpenAPIReference, type OpenAPIReferenceDecoded, type OpenAPIReferencePath, type OpenAPIReferenceResolved, type OpenAPIResolved,
|
95
|
+
export { type OpenAPIReference, type OpenAPIReferenceDecoded, type OpenAPIReferencePath, type OpenAPIReferenceResolved, type OpenAPIResolved, isReferenceObject, resolveDocument, resolveReference };
|
package/dist/openapi.js
CHANGED
@@ -1,15 +1,4 @@
|
|
1
|
-
import { g,
|
2
|
-
function getOperationByRoute(specification, name) {
|
3
|
-
if (!specification || typeof specification != "object" || specification === null || !("paths" in specification) || typeof specification.paths != "object" || specification.paths === null)
|
4
|
-
throw new Error("Missing paths object in the OpenAPI specification.");
|
5
|
-
const match = /^(get|post|put|patch|delete|head|options) (\/.+)$/i.exec(name);
|
6
|
-
if (!match) throw new Error("Could not resolve the path and method from the route name.");
|
7
|
-
const [, routeMethod, routePath] = match, method = routeMethod.toLowerCase(), path = specification.paths[routePath];
|
8
|
-
if (!path) throw new Error(`Route "${name}" not found in specification.`);
|
9
|
-
if (typeof path != "object" || path === null) throw new Error("Invalid path object in the OpenAPI specification.");
|
10
|
-
if (!(method in path)) throw new Error(`Method "${method}" not found in path "${routePath}".`);
|
11
|
-
return { ...path[method], method, path: routePath };
|
12
|
-
}
|
1
|
+
import { g, r } from "./chunks/Biic1J5b.js";
|
13
2
|
function isReferenceObject(value) {
|
14
3
|
return typeof value == "object" && value !== null && "$ref" in value && typeof value.$ref == "string";
|
15
4
|
}
|
@@ -38,11 +27,10 @@ function resolveDocument(value, document = value) {
|
|
38
27
|
});
|
39
28
|
}
|
40
29
|
export {
|
41
|
-
g as
|
42
|
-
a as getOperationById,
|
43
|
-
getOperationByRoute,
|
30
|
+
g as getServerUrl,
|
44
31
|
isReferenceObject,
|
45
32
|
resolveDocument,
|
33
|
+
r as resolveOperation,
|
46
34
|
resolveReference
|
47
35
|
};
|
48
36
|
//# sourceMappingURL=openapi.js.map
|
package/dist/openapi.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"openapi.js","sources":["../openapi/getOperationByRoute.ts","../openapi/isReferenceObject.ts","../openapi/resolveReference.ts","../openapi/resolveDocument.ts"],"sourcesContent":["import type { OpenAPI } from 'openapi-types'\nimport type { OpenAPIV2 } from './OpenApiV2'\n\n/**\n * Given an OpenAPI specification, find a route by its name.\n *\n * @param specification The OpenAPI specification.\n * @param name The name of the route to resolve.\n * @returns The resolved route.\n * @example getOperationByRoute(specification, 'GET /users') // { method: 'get', path: '/users', ... }\n */\nexport function getOperationByRoute<\n T extends object,\n U extends OpenAPIV2.Route<T>,\n>(\n specification: Readonly<T>,\n name: U,\n): OpenAPIV2.OperationByRoute<T, U> {\n\n // --- Assert the specification has a paths object.\n if (!specification\n || typeof specification !== 'object'\n || specification === null\n || 'paths' in specification === false\n || typeof specification.paths !== 'object'\n || specification.paths === null)\n throw new Error('Missing paths object in the OpenAPI specification.')\n\n // --- Extract the path and method from the name.\n const match = /^(get|post|put|patch|delete|head|options) (\\/.+)$/i.exec(name)\n if (!match) throw new Error('Could not resolve the path and method from the route name.')\n const [, routeMethod, routePath] = match\n const method = routeMethod.toLowerCase()\n\n // --- Search for the route in the specification's paths.\n const paths = specification.paths as Record<string, OpenAPI.Operation>\n const path = paths[routePath]\n if (!path) throw new Error(`Route \"${name}\" not found in specification.`)\n if (typeof path !== 'object' || path === null) throw new Error('Invalid path object in the OpenAPI specification.')\n if (method in path === false) throw new Error(`Method \"${method}\" not found in path \"${routePath}\".`)\n const operation = path[method as keyof typeof path] as OpenAPI.Operation\n return { ...operation, method, path: routePath } as OpenAPIV2.OperationByRoute<T, U>\n}\n","import type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\nexport type OpenAPIReference =\n | OpenAPIV2.ReferenceObject\n | OpenAPIV3.ReferenceObject\n | OpenAPIV3_1.ReferenceObject\n\n/**\n * Check if a value is an {@linkcode OpenAPIReference}.\n *\n * @param value The value to check.\n * @returns `true` if the value is a reference object.\n * @example isReferenceObject({ $ref: '#/components/schemas/MySchema' }) // true\n */\nexport function isReferenceObject<T extends OpenAPIReference>(value: unknown): value is T {\n return typeof value === 'object'\n && value !== null\n && '$ref' in value\n && typeof value.$ref === 'string'\n}\n","import type { StringJoin, StringReplace, WriteableDeep } from '@unshared/types'\nimport type { OpenAPIReference } from './isReferenceObject'\nimport { isReferenceObject } from './isReferenceObject'\n\n/**\n * Decode an OpenAPI reference path by replacing the encoded characters with\n * their original values. This function will replace `~0` with `~` and `~1`\n * with `/`.\n *\n * @example DecodeReference<'#/foo~1bar~0baz'> // '#/foo/bar~baz'\n */\nexport type OpenAPIReferenceDecoded<T extends string> =\n StringReplace<StringReplace<T, '~0', '~'>, '~1', '/'>\n\n/**\n * Extract the parts of a reference path as a tuple.\n *\n * @example OpenAPIV3ReferencePath<'#/paths/~1users~1{username}'> // ['paths', '/users/{username}']\n */\nexport type OpenAPIReferencePath<T extends string> =\n T extends `#/${infer P}/${infer Rest}` ? [P, ...OpenAPIReferencePath<Rest>]\n : T extends `#/${infer P}` ? [P]\n : T extends `${infer P}/${infer Rest}` ? [P, ...OpenAPIReferencePath<Rest>]\n : T extends `${infer P}` ? [P]\n : []\n\n/**\n * Resolve a type to the type it references. If the source is not a reference,\n * the source will be returned.\n *\n * @template T The type to resolve.\n * @returns The result type.\n * @example Resolved<{ $ref: '#/info' }, { info: { title: string } }> // { title: string }\n */\nexport type OpenAPIReferenceResolved<\n T extends OpenAPIReference,\n D extends object,\n> =\n D extends object\n ? T extends { $ref: infer R extends string }\n\n // --- Match last part of the reference.\n ? OpenAPIReferencePath<R> extends [infer P extends string]\n ? OpenAPIReferenceDecoded<P> extends keyof D\n ? D[OpenAPIReferenceDecoded<P>] extends object\n ? WriteableDeep<Omit<D[OpenAPIReferenceDecoded<P>], keyof T>>\n : never\n : never\n\n // --- Match middle part of the reference.\n : OpenAPIReferencePath<R> extends [infer P extends string, ...infer Rest extends string[]]\n ? OpenAPIReferenceDecoded<P> extends keyof D\n ? D[OpenAPIReferenceDecoded<P>] extends object\n ? OpenAPIReferenceResolved<{ $ref: StringJoin<Rest, '/'> }, D[OpenAPIReferenceDecoded<P>]>\n : never\n : never\n : never\n : never\n : never\n\n/**\n * Resolve an OpenAPI `ReferenceObject` to the component it references. If the\n * source is not a reference, the source will be returned.\n *\n * @private\n * @param reference The reference object to resolve.\n * @param document The OpenAPI document to resolve the reference from.\n * @returns The result component.\n * @example resolveReference({ $ref: '#/components/schemas/User' }, document)\n */\nexport function resolveReference<\n T extends OpenAPIReference,\n D extends object,\n>(reference: Readonly<T>, document: Readonly<D>): OpenAPIReferenceResolved<T, D> {\n\n // --- Return the source if it is not a reference.\n if (!isReferenceObject(reference))\n throw new TypeError('Expected value to be an OpenAPI reference object.')\n\n // --- Assert that the parameters are valid.\n if (typeof document !== 'object' || document === null)\n throw new TypeError('Expected OpenAPI specification to be an object.')\n\n // --- Resolve the component with it's reference path.\n const referenceParts = reference.$ref.replace(/^#\\//, '').split('/')\n let result = document\n for (const part of referenceParts) {\n if (result === undefined) break\n if (typeof result !== 'object' || result === null) break\n const key = part.replaceAll('~1', '/').replaceAll('~0', '~')\n // @ts-expect-error: assume the part is a key of the object.\n result = result[key] as unknown\n }\n\n // --- Throw an error if the component could not be result.\n if (result === undefined)\n throw new Error(`Could not resolve OpenAPI component: ${reference.$ref}`)\n\n // --- Return the result component.\n return result as OpenAPIReferenceResolved<T, D>\n}\n","import type { ObjectLike, Substract } from '@unshared/types'\nimport type { OpenAPIReference } from './isReferenceObject'\nimport type { OpenAPIReferenceResolved } from './resolveReference'\nimport { isReferenceObject } from './isReferenceObject'\nimport { resolveReference as resolveReference } from './resolveReference'\n\n/**\n * Resolve a type to the type it references. If the source is not a reference,\n * the source will be returned.\n *\n * @template T The type to resolve.\n * @returns The resolved type.\n * @example\n * type Resolved = OpenAPIResolved<{\n * ...\n * paths: {\n * '/users': {\n * get: { $ref: '#/components/routes/getUsers' }\n * }\n * }\n * }>\n */\nexport type OpenAPIResolved<T, D = T, N extends number = 8> =\n N extends 0 ? T\n : T extends OpenAPIReference\n ? D extends object ? OpenAPIResolved<OpenAPIReferenceResolved<T, D>, D, Substract<N, 1>> : never\n : T extends object ? { -readonly [K in keyof T]: OpenAPIResolved<T[K], D, N> } : T\n\n/**\n * Recursively resolve all references in an OpenAPI specification. This function\n * will return a `Proxy` object that will resolve references on the fly.\n *\n * @param value The OpenAPI specification.\n * @returns The resolved OpenAPI specification.\n * @example\n * const resolved = resolveReferences({\n * ...\n * paths: {\n * '/users': {\n * get: { $ref: '#/components/routes/getUsers' },\n * },\n * },\n * })\n */\nexport function resolveDocument<T extends object>(value: Readonly<T>): OpenAPIResolved<T>\nexport function resolveDocument<T extends object, D>(value: Readonly<T>, document: Readonly<D>): OpenAPIResolved<T, D>\nexport function resolveDocument(value: Readonly<ObjectLike>, document = value): unknown {\n return new Proxy(value, {\n get(target, property: string) {\n let value = target[property]\n\n // --- Abort if no document is provided.\n if (!document) return value\n\n // --- Resolve the reference if it is a reference object.\n if (isReferenceObject(value))\n value = resolveReference(value, document)\n\n // --- Recursively resolve references in objects.\n if (typeof value === 'object' && value !== null)\n return resolveDocument(value as ObjectLike, document)\n\n // --- Return the value as is.\n return value\n },\n })\n}\n"],"names":["value"],"mappings":";AAWgB,SAAA,oBAId,eACA,MACkC;AAGlC,MAAI,CAAC,iBACA,OAAO,iBAAkB,YACzB,kBAAkB,QAClB,EAAW,WAAA,kBACX,OAAO,cAAc,SAAU,YAC/B,cAAc,UAAU;AACrB,UAAA,IAAI,MAAM,oDAAoD;AAGhE,QAAA,QAAQ,qDAAqD,KAAK,IAAI;AAC5E,MAAI,CAAC,MAAa,OAAA,IAAI,MAAM,4DAA4D;AACxF,QAAM,GAAG,aAAa,SAAS,IAAI,OAC7B,SAAS,YAAY,YAAY,GAIjC,OADQ,cAAc,MACT,SAAS;AAC5B,MAAI,CAAC,KAAM,OAAM,IAAI,MAAM,UAAU,IAAI,+BAA+B;AACpE,MAAA,OAAO,QAAS,YAAY,SAAS,KAAY,OAAA,IAAI,MAAM,mDAAmD;AAC9G,MAAA,EAAA,UAAU,MAAsB,OAAA,IAAI,MAAM,WAAW,MAAM,wBAAwB,SAAS,IAAI;AAEpG,SAAO,EAAE,GADS,KAAK,MAA2B,GAC3B,QAAQ,MAAM,UAAU;AACjD;AC5BO,SAAS,kBAA8C,OAA4B;AACjF,SAAA,OAAO,SAAU,YACnB,UAAU,QACV,UAAU,SACV,OAAO,MAAM,QAAS;AAC7B;ACmDgB,SAAA,iBAGd,WAAwB,UAAuD;AAG3E,MAAA,CAAC,kBAAkB,SAAS;AACxB,UAAA,IAAI,UAAU,mDAAmD;AAGrE,MAAA,OAAO,YAAa,YAAY,aAAa;AACzC,UAAA,IAAI,UAAU,iDAAiD;AAGjE,QAAA,iBAAiB,UAAU,KAAK,QAAQ,QAAQ,EAAE,EAAE,MAAM,GAAG;AACnE,MAAI,SAAS;AACb,aAAW,QAAQ,gBAAgB;AAEjC,QADI,WAAW,UACX,OAAO,UAAW,YAAY,WAAW,KAAM;AAC7C,UAAA,MAAM,KAAK,WAAW,MAAM,GAAG,EAAE,WAAW,MAAM,GAAG;AAE3D,aAAS,OAAO,GAAG;AAAA,EAAA;AAIrB,MAAI,WAAW;AACb,UAAM,IAAI,MAAM,wCAAwC,UAAU,IAAI,EAAE;AAGnE,SAAA;AACT;ACtDgB,SAAA,gBAAgB,OAA6B,WAAW,OAAgB;AAC/E,SAAA,IAAI,MAAM,OAAO;AAAA,IACtB,IAAI,QAAQ,UAAkB;AACxBA,UAAAA,SAAQ,OAAO,QAAQ;AAG3B,aAAK,YAGD,kBAAkBA,MAAK,MACzBA,SAAQ,iBAAiBA,QAAO,QAAQ,IAGtC,OAAOA,UAAU,YAAYA,WAAU,OAClC,gBAAgBA,QAAqB,QAAQ,IAG/CA,UAXeA;AAAAA,IAAA;AAAA,EAYxB,CACD;AACH;"}
|
1
|
+
{"version":3,"file":"openapi.js","sources":["../openapi/isReferenceObject.ts","../openapi/resolveReference.ts","../openapi/resolveDocument.ts"],"sourcesContent":["import type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\nexport type OpenAPIReference =\n | OpenAPIV2.ReferenceObject\n | OpenAPIV3.ReferenceObject\n | OpenAPIV3_1.ReferenceObject\n\n/**\n * Check if a value is an {@linkcode OpenAPIReference}.\n *\n * @param value The value to check.\n * @returns `true` if the value is a reference object.\n * @example isReferenceObject({ $ref: '#/components/schemas/MySchema' }) // true\n */\nexport function isReferenceObject<T extends OpenAPIReference>(value: unknown): value is T {\n return typeof value === 'object'\n && value !== null\n && '$ref' in value\n && typeof value.$ref === 'string'\n}\n","import type { StringJoin, StringReplace, WriteableDeep } from '@unshared/types'\nimport type { OpenAPIReference } from './isReferenceObject'\nimport { isReferenceObject } from './isReferenceObject'\n\n/**\n * Decode an OpenAPI reference path by replacing the encoded characters with\n * their original values. This function will replace `~0` with `~` and `~1`\n * with `/`.\n *\n * @example DecodeReference<'#/foo~1bar~0baz'> // '#/foo/bar~baz'\n */\nexport type OpenAPIReferenceDecoded<T extends string> =\n StringReplace<StringReplace<T, '~0', '~'>, '~1', '/'>\n\n/**\n * Extract the parts of a reference path as a tuple.\n *\n * @example OpenAPIV3ReferencePath<'#/paths/~1users~1{username}'> // ['paths', '/users/{username}']\n */\nexport type OpenAPIReferencePath<T extends string> =\n T extends `#/${infer P}/${infer Rest}` ? [P, ...OpenAPIReferencePath<Rest>]\n : T extends `#/${infer P}` ? [P]\n : T extends `${infer P}/${infer Rest}` ? [P, ...OpenAPIReferencePath<Rest>]\n : T extends `${infer P}` ? [P]\n : []\n\n/**\n * Resolve a type to the type it references. If the source is not a reference,\n * the source will be returned.\n *\n * @template T The type to resolve.\n * @returns The result type.\n * @example Resolved<{ $ref: '#/info' }, { info: { title: string } }> // { title: string }\n */\nexport type OpenAPIReferenceResolved<\n T extends OpenAPIReference,\n D extends object,\n> =\n D extends object\n ? T extends { $ref: infer R extends string }\n\n // --- Match last part of the reference.\n ? OpenAPIReferencePath<R> extends [infer P extends string]\n ? OpenAPIReferenceDecoded<P> extends keyof D\n ? D[OpenAPIReferenceDecoded<P>] extends object\n ? WriteableDeep<Omit<D[OpenAPIReferenceDecoded<P>], keyof T>>\n : never\n : never\n\n // --- Match middle part of the reference.\n : OpenAPIReferencePath<R> extends [infer P extends string, ...infer Rest extends string[]]\n ? OpenAPIReferenceDecoded<P> extends keyof D\n ? D[OpenAPIReferenceDecoded<P>] extends object\n ? OpenAPIReferenceResolved<{ $ref: StringJoin<Rest, '/'> }, D[OpenAPIReferenceDecoded<P>]>\n : never\n : never\n : never\n : never\n : never\n\n/**\n * Resolve an OpenAPI `ReferenceObject` to the component it references. If the\n * source is not a reference, the source will be returned.\n *\n * @private\n * @param reference The reference object to resolve.\n * @param document The OpenAPI document to resolve the reference from.\n * @returns The result component.\n * @example resolveReference({ $ref: '#/components/schemas/User' }, document)\n */\nexport function resolveReference<\n T extends OpenAPIReference,\n D extends object,\n>(reference: Readonly<T>, document: Readonly<D>): OpenAPIReferenceResolved<T, D> {\n\n // --- Return the source if it is not a reference.\n if (!isReferenceObject(reference))\n throw new TypeError('Expected value to be an OpenAPI reference object.')\n\n // --- Assert that the parameters are valid.\n if (typeof document !== 'object' || document === null)\n throw new TypeError('Expected OpenAPI specification to be an object.')\n\n // --- Resolve the component with it's reference path.\n const referenceParts = reference.$ref.replace(/^#\\//, '').split('/')\n let result = document\n for (const part of referenceParts) {\n if (result === undefined) break\n if (typeof result !== 'object' || result === null) break\n const key = part.replaceAll('~1', '/').replaceAll('~0', '~')\n // @ts-expect-error: assume the part is a key of the object.\n result = result[key] as unknown\n }\n\n // --- Throw an error if the component could not be result.\n if (result === undefined)\n throw new Error(`Could not resolve OpenAPI component: ${reference.$ref}`)\n\n // --- Return the result component.\n return result as OpenAPIReferenceResolved<T, D>\n}\n","import type { ObjectLike, Substract } from '@unshared/types'\nimport type { OpenAPIReference } from './isReferenceObject'\nimport type { OpenAPIReferenceResolved } from './resolveReference'\nimport { isReferenceObject } from './isReferenceObject'\nimport { resolveReference as resolveReference } from './resolveReference'\n\n/**\n * Resolve a type to the type it references. If the source is not a reference,\n * the source will be returned.\n *\n * @template T The type to resolve.\n * @returns The resolved type.\n * @example\n * type Resolved = OpenAPIResolved<{\n * ...\n * paths: {\n * '/users': {\n * get: { $ref: '#/components/routes/getUsers' }\n * }\n * }\n * }>\n */\nexport type OpenAPIResolved<T, D = T, N extends number = 8> =\n N extends 0 ? T\n : T extends OpenAPIReference\n ? D extends object ? OpenAPIResolved<OpenAPIReferenceResolved<T, D>, D, Substract<N, 1>> : never\n : T extends object ? { -readonly [K in keyof T]: OpenAPIResolved<T[K], D, N> } : T\n\n/**\n * Recursively resolve all references in an OpenAPI specification. This function\n * will return a `Proxy` object that will resolve references on the fly.\n *\n * @param value The OpenAPI specification.\n * @returns The resolved OpenAPI specification.\n * @example\n * const resolved = resolveReferences({\n * ...\n * paths: {\n * '/users': {\n * get: { $ref: '#/components/routes/getUsers' },\n * },\n * },\n * })\n */\nexport function resolveDocument<T extends object>(value: Readonly<T>): OpenAPIResolved<T>\nexport function resolveDocument<T extends object, D>(value: Readonly<T>, document: Readonly<D>): OpenAPIResolved<T, D>\nexport function resolveDocument(value: Readonly<ObjectLike>, document = value): unknown {\n return new Proxy(value, {\n get(target, property: string) {\n let value = target[property]\n\n // --- Abort if no document is provided.\n if (!document) return value\n\n // --- Resolve the reference if it is a reference object.\n if (isReferenceObject(value))\n value = resolveReference(value, document)\n\n // --- Recursively resolve references in objects.\n if (typeof value === 'object' && value !== null)\n return resolveDocument(value as ObjectLike, document)\n\n // --- Return the value as is.\n return value\n },\n })\n}\n"],"names":["value"],"mappings":";AAcO,SAAS,kBAA8C,OAA4B;AACjF,SAAA,OAAO,SAAU,YACnB,UAAU,QACV,UAAU,SACV,OAAO,MAAM,QAAS;AAC7B;ACmDgB,SAAA,iBAGd,WAAwB,UAAuD;AAG3E,MAAA,CAAC,kBAAkB,SAAS;AACxB,UAAA,IAAI,UAAU,mDAAmD;AAGrE,MAAA,OAAO,YAAa,YAAY,aAAa;AACzC,UAAA,IAAI,UAAU,iDAAiD;AAGjE,QAAA,iBAAiB,UAAU,KAAK,QAAQ,QAAQ,EAAE,EAAE,MAAM,GAAG;AACnE,MAAI,SAAS;AACb,aAAW,QAAQ,gBAAgB;AAEjC,QADI,WAAW,UACX,OAAO,UAAW,YAAY,WAAW,KAAM;AAC7C,UAAA,MAAM,KAAK,WAAW,MAAM,GAAG,EAAE,WAAW,MAAM,GAAG;AAE3D,aAAS,OAAO,GAAG;AAAA,EAAA;AAIrB,MAAI,WAAW;AACb,UAAM,IAAI,MAAM,wCAAwC,UAAU,IAAI,EAAE;AAGnE,SAAA;AACT;ACtDgB,SAAA,gBAAgB,OAA6B,WAAW,OAAgB;AAC/E,SAAA,IAAI,MAAM,OAAO;AAAA,IACtB,IAAI,QAAQ,UAAkB;AACxBA,UAAAA,SAAQ,OAAO,QAAQ;AAG3B,aAAK,YAGD,kBAAkBA,MAAK,MACzBA,SAAQ,iBAAiBA,QAAO,QAAQ,IAGtC,OAAOA,UAAU,YAAYA,WAAU,OAClC,gBAAgBA,QAAqB,QAAQ,IAG/CA,UAXeA;AAAAA,IAAA;AAAA,EAYxB,CACD;AACH;"}
|
package/dist/utils.cjs
CHANGED
@@ -1,16 +1,18 @@
|
|
1
1
|
"use strict";
|
2
|
-
var handleResponse = require("./chunks/
|
2
|
+
var handleResponse = require("./chunks/Cayg8606.cjs"), parseRequestQuery = require("./chunks/CYmaYL5B.cjs"), request = require("./chunks/Du_W5H6e.cjs");
|
3
3
|
require("@unshared/functions/awaitable");
|
4
|
+
exports.fetch = handleResponse.fetch;
|
4
5
|
exports.handleResponse = handleResponse.handleResponse;
|
5
6
|
exports.handleResponseStreamJson = handleResponse.handleResponseStreamJson;
|
6
|
-
exports.isFormDataLike =
|
7
|
-
exports.isObjectLike =
|
8
|
-
exports.parseRequest =
|
9
|
-
exports.parseRequestBody =
|
10
|
-
exports.parseRequestHeaders =
|
11
|
-
exports.
|
12
|
-
exports.
|
13
|
-
exports.
|
14
|
-
exports.
|
15
|
-
exports.toSearchParams =
|
7
|
+
exports.isFormDataLike = handleResponse.isFormDataLike;
|
8
|
+
exports.isObjectLike = handleResponse.isObjectLike;
|
9
|
+
exports.parseRequest = handleResponse.parseRequest;
|
10
|
+
exports.parseRequestBody = handleResponse.parseRequestBody;
|
11
|
+
exports.parseRequestHeaders = handleResponse.parseRequestHeaders;
|
12
|
+
exports.parseRequestUrl = handleResponse.parseRequestUrl;
|
13
|
+
exports.toFormData = handleResponse.toFormData;
|
14
|
+
exports.parseRequestParameters = parseRequestQuery.parseRequestParameters;
|
15
|
+
exports.parseRequestQuery = parseRequestQuery.parseRequestQuery;
|
16
|
+
exports.toSearchParams = parseRequestQuery.toSearchParams;
|
17
|
+
exports.request = request.request;
|
16
18
|
//# sourceMappingURL=utils.cjs.map
|
package/dist/utils.cjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"utils.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"utils.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;"}
|
package/dist/utils.d.ts
CHANGED
@@ -1,10 +1,31 @@
|
|
1
|
-
import { R as
|
2
|
-
export {
|
1
|
+
import { a as FetchOptions, R as RequestOptions, b as RequestContext } from './chunks/CO11DuYE.js';
|
2
|
+
export { c as FetchHeaders, F as FetchMethod, d as SearchArrayFormat, S as SearchParamsObject, T as ToSearchParamsOptions, p as parseRequest, r as request, t as toSearchParams } from './chunks/CO11DuYE.js';
|
3
3
|
import { Awaitable } from '@unshared/functions/awaitable';
|
4
|
-
import {
|
5
|
-
|
6
|
-
import '
|
7
|
-
|
4
|
+
import { ObjectLike } from '@unshared/types';
|
5
|
+
import './HttpHeaders.js';
|
6
|
+
import './HttpMethods.js';
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Fetch a route with the provided options. This function will parse the route and
|
10
|
+
* options to create a `Request` object and return the response from the server.
|
11
|
+
*
|
12
|
+
* @param route The name of the route to fetch.
|
13
|
+
* @param options The options to pass to the request.
|
14
|
+
* @returns The response from the server.
|
15
|
+
* @example fetch('GET /users', { query: { limit: 10 } })
|
16
|
+
*/
|
17
|
+
declare function fetch(route: string, options?: FetchOptions): Promise<Response>;
|
18
|
+
|
19
|
+
/**
|
20
|
+
* Handle a request response. This function will parse the response based on the content type and
|
21
|
+
* return the data. If an error occurs, the `onError` callback will be called and the function will
|
22
|
+
* throw an error.
|
23
|
+
*
|
24
|
+
* @param response The response to handle.
|
25
|
+
* @param options The options to pass to the request.
|
26
|
+
* @returns The parsed data from the response.
|
27
|
+
*/
|
28
|
+
declare function handleResponse(response: Response, options?: RequestOptions): Promise<unknown>;
|
8
29
|
|
9
30
|
/**
|
10
31
|
* Handle a request response where the content type is a stream of JSON objects. This function
|
@@ -15,7 +36,7 @@ import './chunks/8BFCFxqa.js';
|
|
15
36
|
* @param options The options to pass to the request.
|
16
37
|
* @returns An awaitable iterator that yields the parsed JSON objects.
|
17
38
|
*/
|
18
|
-
declare function handleResponseStreamJson
|
39
|
+
declare function handleResponseStreamJson(response: Response, options: RequestOptions): Awaitable<AsyncIterable<unknown>, unknown[]>;
|
19
40
|
|
20
41
|
/**
|
21
42
|
* A type that represents a FormData-like object, which is a plain object with
|
@@ -39,62 +60,71 @@ declare function isFormDataLike(value: unknown): value is FormDataLike;
|
|
39
60
|
* @returns `true` if the value is an object-like value, `false` otherwise.
|
40
61
|
* @example isObjectLike({}) // true
|
41
62
|
*/
|
42
|
-
declare function isObjectLike(value: unknown): value is
|
63
|
+
declare function isObjectLike(value: unknown): value is ObjectLike;
|
43
64
|
|
44
65
|
/**
|
45
66
|
* Parse the request body based on the provided data and options.
|
46
67
|
*
|
47
|
-
* @param route The route path.
|
48
|
-
* @param options The request options.
|
49
68
|
* @param context The request context.
|
69
|
+
* @param options The request options.
|
50
70
|
*/
|
51
|
-
declare function parseRequestBody(
|
71
|
+
declare function parseRequestBody(context: RequestContext, options: FetchOptions): void;
|
52
72
|
|
53
73
|
/**
|
54
74
|
* Parse the request headers based on the provided data and options.
|
55
75
|
*
|
56
|
-
* @param route The route path.
|
57
|
-
* @param options The request options.
|
58
76
|
* @param context The request context.
|
77
|
+
* @param options The request options.
|
78
|
+
* @example
|
79
|
+
*
|
80
|
+
* // Append the `Content-Type` header to the request.
|
81
|
+
* const context = {}
|
82
|
+
* parseRequestHeaders(context, { headers: { 'Content-Type': 'application/json' } })
|
83
|
+
*
|
84
|
+
* // Will mutate the `init` object to include the headers.
|
85
|
+
* console.log(context) // => { init: { headers: { 'Content-Type': 'application/json' } } }
|
59
86
|
*/
|
60
|
-
declare function parseRequestHeaders(
|
87
|
+
declare function parseRequestHeaders(context: RequestContext, options: FetchOptions): void;
|
61
88
|
|
62
89
|
/**
|
63
|
-
* Parse the request parameters from the request data. This function will
|
64
|
-
* the
|
90
|
+
* Parse the request parameters from the request data. This function will mutate the
|
91
|
+
* `url` object of the context to include the path parameters based on the provided data.
|
65
92
|
*
|
66
|
-
* @param
|
93
|
+
* @param context The request context to mutate.
|
67
94
|
* @param options The options to pass to the request.
|
68
|
-
* @param context The request context to modify.
|
69
95
|
* @example
|
96
|
+
*
|
70
97
|
* // Using `express` style path parameters.
|
71
|
-
*
|
98
|
+
* const context = { url: new URL('https://api.example.com/users/:id') }
|
99
|
+
* parseRequestParameters(context, { parameters: { id: 1 } })
|
100
|
+
* console.log(context.url.pathname) // 'https://api.example.com/users/1'
|
72
101
|
*
|
73
102
|
* // Using `OpenAPI` style path parameters.
|
74
|
-
*
|
103
|
+
* const context = { url: new URL('https://api.example.com/users/{id}') }
|
104
|
+
* parseRequestParameters(context, { parameters: { id: 1 } })
|
105
|
+
* console.log(context.url.pathname) // 'https://api.example.com/users/1'
|
75
106
|
*/
|
76
|
-
declare function parseRequestParameters(
|
107
|
+
declare function parseRequestParameters(context: RequestContext, options: FetchOptions): void;
|
77
108
|
|
78
109
|
/**
|
79
110
|
* Parse the query parameters from the request data. This function will append
|
80
111
|
* the query parameters to the URL based on the method and the data provided.
|
81
112
|
*
|
82
|
-
* @param route The name of the route to fetch. (ignored)
|
83
|
-
* @param options The options to pass to the request.
|
84
113
|
* @param context The request context to modify.
|
114
|
+
* @param options The options to pass to the request.
|
85
115
|
*/
|
86
|
-
declare function parseRequestQuery(
|
116
|
+
declare function parseRequestQuery(context: RequestContext, options: FetchOptions): void;
|
87
117
|
|
88
118
|
/**
|
89
119
|
* Parses the route name to extract the URL and method. It allows the url and method to be
|
90
120
|
* provided in the route name, or in the options object. The method will default to 'get'.
|
91
121
|
*
|
122
|
+
* @param context The request context to mutate.
|
92
123
|
* @param route The name of the route to fetch.
|
93
124
|
* @param options The options to pass to the request.
|
94
|
-
* @param context The request context to modify.
|
95
125
|
* @example parseRequestUrl('GET /users', { baseUrl: 'https://api.example.com' }, context)
|
96
126
|
*/
|
97
|
-
declare function parseRequestUrl(
|
127
|
+
declare function parseRequestUrl(context: RequestContext, route: string, options: FetchOptions): void;
|
98
128
|
|
99
129
|
/**
|
100
130
|
* Casts an object that may contain `Blob`, `File`, or `FileList` values to a `FormData` object.
|
@@ -104,4 +134,4 @@ declare function parseRequestUrl(route: string, options: Pick<RequestOptions, 'b
|
|
104
134
|
*/
|
105
135
|
declare function toFormData(object: FormDataLike): FormData;
|
106
136
|
|
107
|
-
export { type FormDataLike, RequestContext,
|
137
|
+
export { FetchOptions, type FormDataLike, RequestContext, RequestOptions, fetch, handleResponse, handleResponseStreamJson, isFormDataLike, isObjectLike, parseRequestBody, parseRequestHeaders, parseRequestParameters, parseRequestQuery, parseRequestUrl, toFormData };
|
package/dist/utils.js
CHANGED
@@ -1,18 +1,21 @@
|
|
1
|
-
import { h, a } from "./chunks/
|
2
|
-
import {
|
1
|
+
import { f, h, a, i, b, p, c, d, e, t } from "./chunks/BUeqbyph.js";
|
2
|
+
import { p as p2, a as a2, t as t2 } from "./chunks/0ZzUT3m_.js";
|
3
|
+
import { r } from "./chunks/DJJsADWD.js";
|
3
4
|
import "@unshared/functions/awaitable";
|
4
5
|
export {
|
6
|
+
f as fetch,
|
5
7
|
h as handleResponse,
|
6
8
|
a as handleResponseStreamJson,
|
7
9
|
i as isFormDataLike,
|
8
|
-
|
10
|
+
b as isObjectLike,
|
9
11
|
p as parseRequest,
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
c as parseRequestBody,
|
13
|
+
d as parseRequestHeaders,
|
14
|
+
p2 as parseRequestParameters,
|
15
|
+
a2 as parseRequestQuery,
|
16
|
+
e as parseRequestUrl,
|
17
|
+
r as request,
|
15
18
|
t as toFormData,
|
16
|
-
|
19
|
+
t2 as toSearchParams
|
17
20
|
};
|
18
21
|
//# sourceMappingURL=utils.js.map
|
package/dist/utils.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"utils.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"utils.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
|
@@ -0,0 +1,7 @@
|
|
1
|
+
"use strict";
|
2
|
+
var connect = require("./chunks/VkINJoq7.cjs");
|
3
|
+
require("./chunks/CYmaYL5B.cjs");
|
4
|
+
exports.WebSocketChannel = connect.WebSocketChannel;
|
5
|
+
exports.connect = connect.connect;
|
6
|
+
exports.parseConnectOptions = connect.parseConnectOptions;
|
7
|
+
//# sourceMappingURL=websocket.cjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"websocket.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;"}
|
@@ -0,0 +1,148 @@
|
|
1
|
+
import { ObjectLike, Loose, UnionMerge } from '@unshared/types';
|
2
|
+
|
3
|
+
/** The protocols to use for the connection. */
|
4
|
+
type ConnectProtocol = 'WS' | 'WSS';
|
5
|
+
/** Options to pass to the `createChannel` function. */
|
6
|
+
interface ConnectOptions<BaseUrl extends string = string, Query extends ObjectLike = ObjectLike, Parameters extends ObjectLike = ObjectLike, ClientData extends ObjectLike = ObjectLike, ServerData extends ObjectLike = ObjectLike> {
|
7
|
+
/** The protocol to use when connecting to the server. */
|
8
|
+
protocol?: Lowercase<ConnectProtocol> | Uppercase<ConnectProtocol>;
|
9
|
+
/** The base URL to connect to. */
|
10
|
+
baseUrl?: BaseUrl;
|
11
|
+
/**
|
12
|
+
* The path parameters to use when connecting to the server. These parameters will be used to
|
13
|
+
* fill in the path parameters of the connection URL.
|
14
|
+
*
|
15
|
+
* @example { id: 1 }
|
16
|
+
*/
|
17
|
+
parameters?: Parameters;
|
18
|
+
/**
|
19
|
+
* The query parameters to use when connecting to the server. These parameters will be used to
|
20
|
+
* fill in the query parameters of the connection URL.
|
21
|
+
*
|
22
|
+
* @example { limit: 10, offset: 0 }
|
23
|
+
*/
|
24
|
+
query?: Loose<Query>;
|
25
|
+
/**
|
26
|
+
* The data to send when creating the connection. Namely, the path parameters
|
27
|
+
* to use when connecting to the server.
|
28
|
+
*
|
29
|
+
* @example
|
30
|
+
*
|
31
|
+
* // Create a new connection to `http://localhost:8080/users/1`.
|
32
|
+
* connect('GET /users/:id', {
|
33
|
+
* data: { id: 1 },
|
34
|
+
* baseUrl: 'http://localhost:8080'
|
35
|
+
* })
|
36
|
+
*/
|
37
|
+
data?: UnionMerge<Loose<Query> | Parameters>;
|
38
|
+
/**
|
39
|
+
* The payload to send when creating the connection. Namely, the initial message
|
40
|
+
* to send to the server when the connection is established.
|
41
|
+
*/
|
42
|
+
initialPayload?: Loose<ClientData>;
|
43
|
+
/**
|
44
|
+
* Automatically open the connection when it is created. If `true`, the connection
|
45
|
+
* will automatically open when it is created. If `false`, the connection will not
|
46
|
+
* open when it is created.
|
47
|
+
*
|
48
|
+
* @default false
|
49
|
+
*/
|
50
|
+
autoOpen?: boolean;
|
51
|
+
/**
|
52
|
+
* Weather to reconnect the connection when it is closed unexpectedly. If `true`,
|
53
|
+
* the connection will automatically reconnect when it is closed. If `false`, the
|
54
|
+
* connection will not reconnect when it is closed.
|
55
|
+
*
|
56
|
+
* @default false
|
57
|
+
*/
|
58
|
+
autoReconnect?: boolean;
|
59
|
+
/**
|
60
|
+
* The delay in milliseconds to wait before reconnecting the connection. This delay
|
61
|
+
* will be used to wait before reconnecting the connection after it is closed.
|
62
|
+
*
|
63
|
+
* @default 0
|
64
|
+
*/
|
65
|
+
reconnectDelay?: number;
|
66
|
+
/**
|
67
|
+
* The maximum number of times to reconnect the connection before giving up. This
|
68
|
+
* number will be used to determine when to stop trying to reconnect the connection.
|
69
|
+
*
|
70
|
+
* @default 3
|
71
|
+
*/
|
72
|
+
reconnectLimit?: number;
|
73
|
+
/**
|
74
|
+
* The function to call when the connection is opened. This function will be called
|
75
|
+
* when the connection is successfully opened or reconnected.
|
76
|
+
*/
|
77
|
+
onOpen?: (event: Event) => void;
|
78
|
+
/**
|
79
|
+
* The function to call when the connection is closed with an error. This function will
|
80
|
+
* be called when the connection is closed unexpectedly with an error.
|
81
|
+
*/
|
82
|
+
onError?: (event: Event) => void;
|
83
|
+
/**
|
84
|
+
* The function to call when the connection is closed. This function will be called
|
85
|
+
* when the connection is closed unexpectedly or when the connection is closed manually.
|
86
|
+
*/
|
87
|
+
onClose?: (event: CloseEvent) => void;
|
88
|
+
/**
|
89
|
+
* The function to call when a message is received from the server. This function will
|
90
|
+
* be called when a message is received from the server.
|
91
|
+
*/
|
92
|
+
onMessage?: (data: ServerData) => void;
|
93
|
+
}
|
94
|
+
interface WebSocketParameters {
|
95
|
+
url: URL;
|
96
|
+
protocol?: 'ws' | 'wss';
|
97
|
+
}
|
98
|
+
declare function parseConnectOptions(channel: string, options: ConnectOptions): WebSocketParameters;
|
99
|
+
|
100
|
+
type RemoveListener = () => void;
|
101
|
+
type ClientData<T extends ConnectOptions> = T extends ConnectOptions<any, any, any, infer R extends ObjectLike, any> ? R : ObjectLike;
|
102
|
+
type ServerData<T extends ConnectOptions> = T extends ConnectOptions<any, any, any, any, infer R extends ObjectLike> ? R : ObjectLike;
|
103
|
+
declare class WebSocketChannel<T extends ConnectOptions = ConnectOptions> {
|
104
|
+
channel: string;
|
105
|
+
options: T;
|
106
|
+
constructor(channel: string, options: T);
|
107
|
+
/** The WebSocket connection to the server. */
|
108
|
+
webSocket: WebSocket | undefined;
|
109
|
+
/**
|
110
|
+
* Open a new WebSocket connection to the server. The connection will be opened with the given
|
111
|
+
* URL and protocols. If the connection is already open, the connection will be closed before
|
112
|
+
* opening a new connection. Also add the event listeners that were passed in the options.
|
113
|
+
*/
|
114
|
+
open(): Promise<void>;
|
115
|
+
/**
|
116
|
+
* Send a payload to the server. The payload will be serialized to JSON before sending.
|
117
|
+
*
|
118
|
+
* @param payload The data to send to the server.
|
119
|
+
*/
|
120
|
+
send(payload: ClientData<T>): void;
|
121
|
+
/**
|
122
|
+
* Listen for events from the server. The event will be deserialized from JSON before calling the callback.
|
123
|
+
*
|
124
|
+
* @param event The event to listen for.
|
125
|
+
* @param callback The callback to call when the event is received.
|
126
|
+
* @returns A function to remove the event listener.
|
127
|
+
*/
|
128
|
+
on(event: 'message', callback: (data: ServerData<T>) => void): RemoveListener;
|
129
|
+
on(event: 'close', callback: (event: CloseEvent) => void): RemoveListener;
|
130
|
+
on(event: 'error', callback: (event: Event) => void): RemoveListener;
|
131
|
+
on(event: 'open', callback: (event: Event) => void): RemoveListener;
|
132
|
+
/**
|
133
|
+
* Close the WebSocket connection to the server. The connection will not be able to send or receive
|
134
|
+
* messages after it is closed.
|
135
|
+
*/
|
136
|
+
close(): Promise<void>;
|
137
|
+
}
|
138
|
+
/**
|
139
|
+
* Create a new WebSocket connection to the server with the given path. The connection will
|
140
|
+
* automatically reconnect if the connection is closed unexpectedly.
|
141
|
+
*
|
142
|
+
* @param route The name of the route to connect to.
|
143
|
+
* @param options The options to pass to the connection.
|
144
|
+
* @returns The WebSocket connection.
|
145
|
+
*/
|
146
|
+
declare function connect(route: string, options: ConnectOptions): WebSocketChannel;
|
147
|
+
|
148
|
+
export { type ConnectOptions, type ConnectProtocol, WebSocketChannel, type WebSocketParameters, connect, parseConnectOptions };
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"websocket.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
|