@unshared/client 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ var index = require("./chunks/BzqHK4CV.cjs");
3
+ exports.getBaseUrl = index.getBaseUrl;
4
+ exports.getOperationById = index.getOperationById;
5
+ exports.getOperationByRoute = index.getOperationByRoute;
6
+ exports.isReferenceObject = index.isReferenceObject;
7
+ exports.resolveDocument = index.resolveDocument;
8
+ exports.resolveReference = index.resolveReference;
9
+ //# sourceMappingURL=openapi.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openapi.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;"}
@@ -0,0 +1,123 @@
1
+ import { OpenAPI, OpenAPIV2 as OpenAPIV2$1, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types';
2
+ import { O as OpenAPIV2 } from './chunks/BIx2AaJH.js';
3
+ export { a as OpenAPIV3 } from './chunks/BIx2AaJH.js';
4
+ import { StringReplace, WriteableDeep, StringJoin } from '@unshared/types';
5
+ import './chunks/8BFCFxqa.js';
6
+
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;
37
+ /**
38
+ * Check if a value is an {@linkcode OpenAPIReference}.
39
+ *
40
+ * @param value The value to check.
41
+ * @returns `true` if the value is a reference object.
42
+ * @example isReferenceObject({ $ref: '#/components/schemas/MySchema' }) // true
43
+ */
44
+ declare function isReferenceObject<T extends OpenAPIReference>(value: unknown): value is T;
45
+
46
+ /**
47
+ * Decode an OpenAPI reference path by replacing the encoded characters with
48
+ * their original values. This function will replace `~0` with `~` and `~1`
49
+ * with `/`.
50
+ *
51
+ * @example DecodeReference<'#/foo~1bar~0baz'> // '#/foo/bar~baz'
52
+ */
53
+ type OpenAPIReferenceDecoded<T extends string> = StringReplace<StringReplace<T, '~0', '~'>, '~1', '/'>;
54
+ /**
55
+ * Extract the parts of a reference path as a tuple.
56
+ *
57
+ * @example OpenAPIV3ReferencePath<'#/paths/~1users~1{username}'> // ['paths', '/users/{username}']
58
+ */
59
+ type OpenAPIReferencePath<T extends string> = T extends `#/${infer P}/${infer Rest}` ? [P, ...OpenAPIReferencePath<Rest>] : T extends `#/${infer P}` ? [P] : T extends `${infer P}/${infer Rest}` ? [P, ...OpenAPIReferencePath<Rest>] : T extends `${infer P}` ? [P] : [];
60
+ /**
61
+ * Resolve a type to the type it references. If the source is not a reference,
62
+ * the source will be returned.
63
+ *
64
+ * @template T The type to resolve.
65
+ * @returns The result type.
66
+ * @example Resolved<{ $ref: '#/info' }, { info: { title: string } }> // { title: string }
67
+ */
68
+ type OpenAPIReferenceResolved<T extends OpenAPIReference, D extends object> = D extends object ? T extends {
69
+ $ref: infer R extends string;
70
+ } ? OpenAPIReferencePath<R> extends [infer P extends string] ? OpenAPIReferenceDecoded<P> extends keyof D ? D[OpenAPIReferenceDecoded<P>] extends object ? WriteableDeep<Omit<D[OpenAPIReferenceDecoded<P>], keyof T>> : never : never : OpenAPIReferencePath<R> extends [infer P extends string, ...infer Rest extends string[]] ? OpenAPIReferenceDecoded<P> extends keyof D ? D[OpenAPIReferenceDecoded<P>] extends object ? OpenAPIReferenceResolved<{
71
+ $ref: StringJoin<Rest, '/'>;
72
+ }, D[OpenAPIReferenceDecoded<P>]> : never : never : never : never : never;
73
+ /**
74
+ * Resolve an OpenAPI `ReferenceObject` to the component it references. If the
75
+ * source is not a reference, the source will be returned.
76
+ *
77
+ * @private
78
+ * @param reference The reference object to resolve.
79
+ * @param document The OpenAPI document to resolve the reference from.
80
+ * @returns The result component.
81
+ * @example resolveReference({ $ref: '#/components/schemas/User' }, document)
82
+ */
83
+ declare function resolveReference<T extends OpenAPIReference, D extends object>(reference: Readonly<T>, document: Readonly<D>): OpenAPIReferenceResolved<T, D>;
84
+
85
+ /**
86
+ * Resolve a type to the type it references. If the source is not a reference,
87
+ * the source will be returned.
88
+ *
89
+ * @template T The type to resolve.
90
+ * @returns The resolved type.
91
+ * @example
92
+ * type Resolved = OpenAPIResolved<{
93
+ * ...
94
+ * paths: {
95
+ * '/users': {
96
+ * get: { $ref: '#/components/routes/getUsers' }
97
+ * }
98
+ * }
99
+ * }>
100
+ */
101
+ type OpenAPIResolved<T, D = T> = T extends OpenAPIReference ? D extends object ? OpenAPIResolved<OpenAPIReferenceResolved<T, D>, D> : never : T extends object ? {
102
+ -readonly [K in keyof T]: OpenAPIResolved<T[K], D>;
103
+ } : T;
104
+ /**
105
+ * Recursively resolve all references in an OpenAPI specification. This function
106
+ * will return a `Proxy` object that will resolve references on the fly.
107
+ *
108
+ * @param value The OpenAPI specification.
109
+ * @returns The resolved OpenAPI specification.
110
+ * @example
111
+ * const resolved = resolveReferences({
112
+ * ...
113
+ * paths: {
114
+ * '/users': {
115
+ * get: { $ref: '#/components/routes/getUsers' },
116
+ * },
117
+ * },
118
+ * })
119
+ */
120
+ declare function resolveDocument<T extends object>(value: Readonly<T>): OpenAPIResolved<T>;
121
+ declare function resolveDocument<T extends object, D>(value: Readonly<T>, document: Readonly<D>): OpenAPIResolved<T, D>;
122
+
123
+ export { type OpenAPIReference, type OpenAPIReferenceDecoded, type OpenAPIReferencePath, type OpenAPIReferenceResolved, type OpenAPIResolved, OpenAPIV2, getBaseUrl, getOperationById, getOperationByRoute, isReferenceObject, resolveDocument, resolveReference };
@@ -0,0 +1,10 @@
1
+ import { g, a, b, i, r, c } from "./chunks/CRISqhP7.js";
2
+ export {
3
+ g as getBaseUrl,
4
+ a as getOperationById,
5
+ b as getOperationByRoute,
6
+ i as isReferenceObject,
7
+ r as resolveDocument,
8
+ c as resolveReference
9
+ };
10
+ //# sourceMappingURL=openapi.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openapi.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
package/dist/utils.cjs ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var parseRequest = require("./chunks/C3RyfPUw.cjs");
3
+ exports.isFormDataLike = parseRequest.isFormDataLike;
4
+ exports.isObjectLike = parseRequest.isObjectLike;
5
+ exports.parseRequest = parseRequest.parseRequest;
6
+ exports.parseRequestBody = parseRequest.parseRequestBody;
7
+ exports.parseRequestHeaders = parseRequest.parseRequestHeaders;
8
+ exports.parseRequestParameters = parseRequest.parseRequestParameters;
9
+ exports.parseRequestQuery = parseRequest.parseRequestQuery;
10
+ exports.parseRequestUrl = parseRequest.parseRequestUrl;
11
+ exports.toFormData = parseRequest.toFormData;
12
+ exports.toSearchParams = parseRequest.toSearchParams;
13
+ //# sourceMappingURL=utils.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;"}
@@ -0,0 +1,93 @@
1
+ import { a as RequestOptions, b as RequestContext } from './chunks/B0duX_ls.js';
2
+ export { R as RequestHeaders, c as SearchArrayFormat, S as SearchParamsObject, T as ToSearchParamsOptions, p as parseRequest, t as toSearchParams } from './chunks/B0duX_ls.js';
3
+ import '@unshared/types';
4
+ import './chunks/8BFCFxqa.js';
5
+
6
+ /**
7
+ * A type that represents a FormData-like object, which is a plain object with
8
+ * nested Blob, File, or FileList values. Or a FormData instance.
9
+ */
10
+ type FormDataLike = FormData | Record<string, Blob | File | FileList>;
11
+ /**
12
+ * Predicate to check if a value is FormData-like, meaning it is a plain object
13
+ * with nested Blob, File, or FileList values.
14
+ *
15
+ * @param value The value to check.
16
+ * @returns `true` if the value is FormData-like, `false` otherwise.
17
+ * @example isFormDataLike({ file: new File(['test'], 'test.txt') }) // true
18
+ */
19
+ declare function isFormDataLike(value: unknown): value is FormDataLike;
20
+
21
+ /**
22
+ * Predicate to check if a value is an object-like value.
23
+ *
24
+ * @param value The value to check.
25
+ * @returns `true` if the value is an object-like value, `false` otherwise.
26
+ * @example isObjectLike({}) // true
27
+ */
28
+ declare function isObjectLike(value: unknown): value is Record<string, unknown>;
29
+
30
+ /**
31
+ * Parse the request body based on the provided data and options.
32
+ *
33
+ * @param route The route path.
34
+ * @param options The request options.
35
+ * @param context The request context.
36
+ */
37
+ declare function parseRequestBody(route: string, options: Pick<RequestOptions, 'body' | 'data'>, context: RequestContext): void;
38
+
39
+ /**
40
+ * Parse the request headers based on the provided data and options.
41
+ *
42
+ * @param route The route path.
43
+ * @param options The request options.
44
+ * @param context The request context.
45
+ */
46
+ declare function parseRequestHeaders(route: string, options: Pick<RequestOptions, 'headers'>, context: RequestContext): void;
47
+
48
+ /**
49
+ * Parse the request parameters from the request data. This function will append
50
+ * the path parameters to the URL based on the method and the data provided.
51
+ *
52
+ * @param route The name of the route to fetch. (ignored)
53
+ * @param options The options to pass to the request.
54
+ * @param context The request context to modify.
55
+ * @example
56
+ * // Using `express` style path parameters.
57
+ * parseRequestParameters('GET /users/:id', { data: { id: 1 } }, context)
58
+ *
59
+ * // Using `OpenAPI` style path parameters.
60
+ * parseRequestParameters('GET /users/{id}', { data: { id: 1 } }, context)
61
+ */
62
+ declare function parseRequestParameters(route: string, options: Pick<RequestOptions, 'data' | 'parameters'>, context: RequestContext): void;
63
+
64
+ /**
65
+ * Parse the query parameters from the request data. This function will append
66
+ * the query parameters to the URL based on the method and the data provided.
67
+ *
68
+ * @param route The name of the route to fetch. (ignored)
69
+ * @param options The options to pass to the request.
70
+ * @param context The request context to modify.
71
+ */
72
+ declare function parseRequestQuery(route: string, options: Pick<RequestOptions, 'data' | 'query' | 'searchArrayFormat'>, context: RequestContext): void;
73
+
74
+ /**
75
+ * Parses the route name to extract the URL and method. It allows the url and method to be
76
+ * provided in the route name, or in the options object. The method will default to 'get'.
77
+ *
78
+ * @param route The name of the route to fetch.
79
+ * @param options The options to pass to the request.
80
+ * @param context The request context to modify.
81
+ * @example parseRequestUrl('GET /users', { baseUrl: 'https://api.example.com' }, context)
82
+ */
83
+ declare function parseRequestUrl(route: string, options: Pick<RequestOptions, 'baseUrl' | 'method'>, context: RequestContext): void;
84
+
85
+ /**
86
+ * Casts an object that may contain `Blob`, `File`, or `FileList` values to a `FormData` object.
87
+ *
88
+ * @param object The object to cast to a `FormData` object.
89
+ * @returns The `FormData` object.
90
+ */
91
+ declare function toFormData(object: FormDataLike): FormData;
92
+
93
+ export { type FormDataLike, RequestContext, RequestOptions, isFormDataLike, isObjectLike, parseRequestBody, parseRequestHeaders, parseRequestParameters, parseRequestQuery, parseRequestUrl, toFormData };
package/dist/utils.js ADDED
@@ -0,0 +1,14 @@
1
+ import { i, a, p, b, c, d, e, f, t, g } from "./chunks/wF9C7mO0.js";
2
+ export {
3
+ i as isFormDataLike,
4
+ a as isObjectLike,
5
+ p as parseRequest,
6
+ b as parseRequestBody,
7
+ c as parseRequestHeaders,
8
+ d as parseRequestParameters,
9
+ e as parseRequestQuery,
10
+ f as parseRequestUrl,
11
+ t as toFormData,
12
+ g as toSearchParams
13
+ };
14
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@unshared/client",
3
+ "type": "module",
4
+ "version": "0.2.0",
5
+ "license": "MIT",
6
+ "sideEffects": false,
7
+ "author": "Stanley Horwood <stanley@hsjm.io>",
8
+ "bugs": "https://github.com/shorwood/unshared/issues",
9
+ "homepage": "https://github.com/shorwood/unshared#readme",
10
+ "repository": {
11
+ "directory": "packages/client",
12
+ "type": "git",
13
+ "url": "https://github.com/shorwood/unshared"
14
+ },
15
+ "main": "./dist/index.cjs",
16
+ "module": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ "./createClient": {
20
+ "require": "./dist/createClient.cjs",
21
+ "types": "./dist/createClient.d.ts",
22
+ "import": "./dist/createClient.js"
23
+ },
24
+ "./fetch": {
25
+ "require": "./dist/fetch.cjs",
26
+ "types": "./dist/fetch.d.ts",
27
+ "import": "./dist/fetch.js"
28
+ },
29
+ "./openapi": {
30
+ "require": "./dist/openapi.cjs",
31
+ "types": "./dist/openapi.d.ts",
32
+ "import": "./dist/openapi.js"
33
+ },
34
+ "./utils": {
35
+ "require": "./dist/utils.cjs",
36
+ "types": "./dist/utils.d.ts",
37
+ "import": "./dist/utils.js"
38
+ },
39
+ ".": {
40
+ "require": "./dist/index.cjs",
41
+ "types": "./dist/index.d.ts",
42
+ "import": "./dist/index.js"
43
+ }
44
+ },
45
+ "files": [
46
+ "dist",
47
+ "README.md",
48
+ "LICENSE.md"
49
+ ],
50
+ "dependencies": {
51
+ "@unshared/types": "0.2.0",
52
+ "openapi-types": "12.1.3"
53
+ },
54
+ "devDependencies": {
55
+ "@unshared/scripts": "0.2.0"
56
+ },
57
+ "scripts": {
58
+ "build:httpMethods": "tsx ./scripts/buildHttpMethods.ts",
59
+ "build:httpHeaders": "tsx ./scripts/buildHttpHeaders.ts",
60
+ "build:httpStatusCodes": "tsx ./scripts/buildHttpStatusCodes.ts"
61
+ }
62
+ }