@unshared/client 0.2.2 → 0.3.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/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/B92aAMq0.d.ts +144 -0
- package/dist/chunks/{D-WqCFul.js → Biic1J5b.js} +11 -9
- package/dist/chunks/Biic1J5b.js.map +1 -0
- package/dist/chunks/CfKxYeRr.cjs +155 -0
- package/dist/chunks/CfKxYeRr.cjs.map +1 -0
- package/dist/chunks/CjU0376e.d.ts +229 -0
- package/dist/chunks/{xRZPkxch.cjs → CtW2aMuA.cjs} +11 -9
- package/dist/chunks/CtW2aMuA.cjs.map +1 -0
- package/dist/chunks/D51s1VII.js +156 -0
- package/dist/chunks/D51s1VII.js.map +1 -0
- package/dist/chunks/iA98-4f5.cjs +8 -0
- package/dist/chunks/iA98-4f5.cjs.map +1 -0
- package/dist/chunks/lMH6B5BV.js +9 -0
- package/dist/chunks/lMH6B5BV.js.map +1 -0
- package/dist/createClient.cjs +81 -18
- package/dist/createClient.cjs.map +1 -1
- package/dist/createClient.d.ts +93 -29
- package/dist/createClient.js +83 -20
- package/dist/createClient.js.map +1 -1
- package/dist/createService.cjs +17 -0
- package/dist/createService.cjs.map +1 -0
- package/dist/createService.d.ts +38 -0
- package/dist/createService.js +19 -0
- package/dist/createService.js.map +1 -0
- package/dist/index.cjs +22 -20
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +10 -624
- package/dist/index.js +24 -19
- 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 +11 -9
- package/package.json +23 -8
- package/dist/chunks/B5hc73Po.js +0 -96
- package/dist/chunks/B5hc73Po.js.map +0 -1
- package/dist/chunks/BsMghIPi.d.ts +0 -186
- 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/BsMghIPi.js';
|
1
|
+
export { k as OpenAPIRoutes, O as OpenAPIV2, a as OpenAPIV3, c as Operation, d as OperationById, j as OperationByRoute, b as OperationId, e as OperationOptions, h as OperationResponse, f as OperationResult, i as OperationRoute, S as ServerUrl, g as getServerUrl, r as resolveOperation } from './chunks/CjU0376e.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/B92aAMq0.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/CfKxYeRr.cjs"), request = require("./chunks/iA98-4f5.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.parseRequestParameters =
|
12
|
-
exports.parseRequestQuery =
|
13
|
-
exports.parseRequestUrl =
|
14
|
-
exports.toFormData =
|
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.parseRequestParameters = handleResponse.parseRequestParameters;
|
13
|
+
exports.parseRequestQuery = handleResponse.parseRequestQuery;
|
14
|
+
exports.parseRequestUrl = handleResponse.parseRequestUrl;
|
15
|
+
exports.toFormData = handleResponse.toFormData;
|
16
|
+
exports.toSearchParams = handleResponse.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 { b as FetchOptions, c as RequestOptions, R as RequestContext } from './chunks/B92aAMq0.js';
|
2
|
+
export { a 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/B92aAMq0.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,20 @@
|
|
1
|
-
import { h, a } from "./chunks/
|
2
|
-
import {
|
1
|
+
import { f, h, a, i, b, p, c, d, e, g, j, t, k } from "./chunks/D51s1VII.js";
|
2
|
+
import { r } from "./chunks/lMH6B5BV.js";
|
3
3
|
import "@unshared/functions/awaitable";
|
4
4
|
export {
|
5
|
+
f as fetch,
|
5
6
|
h as handleResponse,
|
6
7
|
a as handleResponseStreamJson,
|
7
8
|
i as isFormDataLike,
|
8
|
-
|
9
|
+
b as isObjectLike,
|
9
10
|
p as parseRequest,
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
c as parseRequestBody,
|
12
|
+
d as parseRequestHeaders,
|
13
|
+
e as parseRequestParameters,
|
14
|
+
g as parseRequestQuery,
|
15
|
+
j as parseRequestUrl,
|
16
|
+
r as request,
|
15
17
|
t as toFormData,
|
16
|
-
|
18
|
+
k as toSearchParams
|
17
19
|
};
|
18
20
|
//# sourceMappingURL=utils.js.map
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@unshared/client",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.
|
4
|
+
"version": "0.3.0",
|
5
5
|
"license": "MIT",
|
6
6
|
"sideEffects": false,
|
7
7
|
"author": "Stanley Horwood <stanley@hsjm.io>",
|
@@ -16,15 +16,30 @@
|
|
16
16
|
"module": "./dist/index.js",
|
17
17
|
"types": "./dist/index.d.ts",
|
18
18
|
"exports": {
|
19
|
+
"./HttpHeaders": {
|
20
|
+
"require": "./dist/HttpHeaders.cjs",
|
21
|
+
"types": "./dist/HttpHeaders.d.ts",
|
22
|
+
"import": "./dist/HttpHeaders.js"
|
23
|
+
},
|
24
|
+
"./HttpMethods": {
|
25
|
+
"require": "./dist/HttpMethods.cjs",
|
26
|
+
"types": "./dist/HttpMethods.d.ts",
|
27
|
+
"import": "./dist/HttpMethods.js"
|
28
|
+
},
|
29
|
+
"./HttpStatusCodes": {
|
30
|
+
"require": "./dist/HttpStatusCodes.cjs",
|
31
|
+
"types": "./dist/HttpStatusCodes.d.ts",
|
32
|
+
"import": "./dist/HttpStatusCodes.js"
|
33
|
+
},
|
19
34
|
"./createClient": {
|
20
35
|
"require": "./dist/createClient.cjs",
|
21
36
|
"types": "./dist/createClient.d.ts",
|
22
37
|
"import": "./dist/createClient.js"
|
23
38
|
},
|
24
|
-
"./
|
25
|
-
"require": "./dist/
|
26
|
-
"types": "./dist/
|
27
|
-
"import": "./dist/
|
39
|
+
"./createService": {
|
40
|
+
"require": "./dist/createService.cjs",
|
41
|
+
"types": "./dist/createService.d.ts",
|
42
|
+
"import": "./dist/createService.js"
|
28
43
|
},
|
29
44
|
"./openapi": {
|
30
45
|
"require": "./dist/openapi.cjs",
|
@@ -48,12 +63,12 @@
|
|
48
63
|
"LICENSE.md"
|
49
64
|
],
|
50
65
|
"dependencies": {
|
51
|
-
"@unshared/functions": "0.
|
52
|
-
"@unshared/types": "0.
|
66
|
+
"@unshared/functions": "0.3.0",
|
67
|
+
"@unshared/types": "0.3.0",
|
53
68
|
"openapi-types": "12.1.3"
|
54
69
|
},
|
55
70
|
"devDependencies": {
|
56
|
-
"@unshared/scripts": "0.
|
71
|
+
"@unshared/scripts": "0.3.0"
|
57
72
|
},
|
58
73
|
"scripts": {
|
59
74
|
"build:httpMethods": "tsx ./scripts/buildHttpMethods.ts",
|
package/dist/chunks/B5hc73Po.js
DELETED
@@ -1,96 +0,0 @@
|
|
1
|
-
function isFormDataLike(value) {
|
2
|
-
if (typeof value != "object" || value === null) return !1;
|
3
|
-
if (value instanceof FormData) return !0;
|
4
|
-
const values = Object.values(value);
|
5
|
-
return values.length === 0 ? !1 : values.every((x) => x instanceof File ? !0 : Array.isArray(x) ? x.every((item) => item instanceof File) : x instanceof Blob);
|
6
|
-
}
|
7
|
-
function isObjectLike(value) {
|
8
|
-
return typeof value == "object" && value !== null && value.constructor === Object;
|
9
|
-
}
|
10
|
-
function toFormData(object) {
|
11
|
-
if (object instanceof FormData) return object;
|
12
|
-
const formData = new FormData();
|
13
|
-
for (const key in object) {
|
14
|
-
const value = object[key];
|
15
|
-
if (value !== void 0)
|
16
|
-
if (Array.isArray(value))
|
17
|
-
for (const item of value)
|
18
|
-
formData.append(key, item);
|
19
|
-
else
|
20
|
-
formData.append(key, value);
|
21
|
-
}
|
22
|
-
return formData;
|
23
|
-
}
|
24
|
-
function parseRequestBody(route, options, context) {
|
25
|
-
const { data, body = data } = options, { init } = context;
|
26
|
-
["get", "head", "delete"].includes(init.method ?? "get") || body != null && (isFormDataLike(body) ? (init.body = toFormData(body), init.headers = init.headers ?? {}, init.headers = { ...init.headers, "Content-Type": "multipart/form-data" }) : body instanceof ReadableStream ? init.body = body : body instanceof File ? (init.body = body.stream(), init.headers = init.headers ?? {}, init.headers = { ...init.headers, "Content-Type": "application/octet-stream" }) : isObjectLike(body) ? (init.body = JSON.stringify(body), init.headers = init.headers ?? {}, init.headers = { ...init.headers, "Content-Type": "application/json" }) : init.body = body);
|
27
|
-
}
|
28
|
-
function parseRequestHeaders(route, options, context) {
|
29
|
-
const { headers = {} } = options, { init } = context;
|
30
|
-
for (const key in headers) {
|
31
|
-
const value = headers[key];
|
32
|
-
value !== void 0 && typeof value == "string" && (init.headers = init.headers ?? {}, init.headers = { ...init.headers, [key]: value });
|
33
|
-
}
|
34
|
-
}
|
35
|
-
const EXP_PATH_PARAMETER = /:([\w-]+)|%7B([\w-]+)%7D/g;
|
36
|
-
function parseRequestParameters(route, options, context) {
|
37
|
-
const { url } = context, { data, parameters = {} } = options;
|
38
|
-
if (!url) throw new Error("Could not resolve the `RequestInit` object: the `url` is missing.");
|
39
|
-
const pathParameters = url.pathname.match(EXP_PATH_PARAMETER);
|
40
|
-
if (pathParameters)
|
41
|
-
for (const parameter of pathParameters.values()) {
|
42
|
-
const key = parameter.replaceAll(EXP_PATH_PARAMETER, "$1$2"), value = parameters[key];
|
43
|
-
value !== void 0 && typeof value == "string" ? url.pathname = url.pathname.replace(parameter, value) : isObjectLike(data) && data[key] !== void 0 && typeof data[key] == "string" && (url.pathname = url.pathname.replace(parameter, data[key]), delete data[key]);
|
44
|
-
}
|
45
|
-
}
|
46
|
-
function toSearchParams(object, options = {}) {
|
47
|
-
const { searchArrayFormat = "flat" } = options, search = new URLSearchParams();
|
48
|
-
for (const key in object) {
|
49
|
-
const value = object[key];
|
50
|
-
if (value !== void 0)
|
51
|
-
if (Array.isArray(value)) {
|
52
|
-
if (searchArrayFormat === "brackets") for (const v of value) search.append(`${key}[]`, String(v));
|
53
|
-
else if (searchArrayFormat === "indices") for (const [i, v] of value.entries()) search.append(`${key}[${i}]`, String(v));
|
54
|
-
else if (searchArrayFormat === "comma") search.append(key, value.join(","));
|
55
|
-
else if (searchArrayFormat === "path") for (const [i, v] of value.entries()) search.append(`${key}.${i}`, String(v));
|
56
|
-
else if (searchArrayFormat === "flat") for (const v of value) search.append(key, String(v));
|
57
|
-
} else
|
58
|
-
search.append(key, value.toString());
|
59
|
-
}
|
60
|
-
return search;
|
61
|
-
}
|
62
|
-
function parseRequestQuery(route, options, context) {
|
63
|
-
const { url, init } = context, { data, query = {}, searchArrayFormat } = options;
|
64
|
-
if (!url) throw new Error("Could not resolve the `RequestInit` object: the `url` is missing.");
|
65
|
-
if (!["post", "put", "patch"].includes(init.method ?? "get") && isObjectLike(data))
|
66
|
-
for (const key in data)
|
67
|
-
data[key] !== void 0 && query[key] === void 0 && (query[key] = data[key], delete data[key]);
|
68
|
-
url.search = toSearchParams(query, { searchArrayFormat }).toString();
|
69
|
-
}
|
70
|
-
const EXP_REQUEST = /^((?<method>[a-z]+) )?(?<url>[^:]+?:\/{2}[^/]+)?(?<path>\/[^\s?]*)/i, METHODS = /* @__PURE__ */ new Set(["get", "post", "put", "patch", "delete", "head", "options"]);
|
71
|
-
function parseRequestUrl(route, options, context) {
|
72
|
-
const { method, baseUrl } = options, match = EXP_REQUEST.exec(route);
|
73
|
-
if (!match?.groups) throw new Error("Could not resolve the `RequestInit` object: Invalid route name.");
|
74
|
-
const routeMethod = method ?? match.groups.method ?? "get", routeBaseUrl = baseUrl ?? match.groups.url;
|
75
|
-
if (!routeBaseUrl) throw new Error("Could not resolve the `RequestInit` object: the `baseUrl` is missing.");
|
76
|
-
const methodLower = routeMethod.toLowerCase();
|
77
|
-
if (!METHODS.has(methodLower)) throw new Error(`Could not resolve the \`RequestInit\` object:, the method \`${routeMethod}\` is invalid.`);
|
78
|
-
context.init = context.init ?? {}, context.init.method = methodLower, context.url = new URL(routeBaseUrl), context.url.pathname += context.url.pathname.endsWith("/") ? match.groups.path.slice(1) : match.groups.path;
|
79
|
-
}
|
80
|
-
function parseRequest(route, options) {
|
81
|
-
const { data, body, query, headers, parameters, baseUrl, method, searchArrayFormat, ...requestInit } = options, context = { init: requestInit };
|
82
|
-
return parseRequestUrl(route, { baseUrl, method }, context), parseRequestParameters(route, { data, parameters }, context), parseRequestQuery(route, { data, query, searchArrayFormat }, context), parseRequestBody(route, { data, body }, context), parseRequestHeaders(route, { headers }, context), context;
|
83
|
-
}
|
84
|
-
export {
|
85
|
-
isObjectLike as a,
|
86
|
-
parseRequestBody as b,
|
87
|
-
parseRequestHeaders as c,
|
88
|
-
parseRequestParameters as d,
|
89
|
-
parseRequestQuery as e,
|
90
|
-
parseRequestUrl as f,
|
91
|
-
toSearchParams as g,
|
92
|
-
isFormDataLike as i,
|
93
|
-
parseRequest as p,
|
94
|
-
toFormData as t
|
95
|
-
};
|
96
|
-
//# sourceMappingURL=B5hc73Po.js.map
|