@kollors/react-codegen 2.0.0-alpha.1 → 2.0.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -1
- package/README.ru.md +60 -1
- package/dist/cli.js +16 -13
- package/dist/cli.js.map +1 -1
- package/dist/generate.d.ts +1 -0
- package/dist/generate.js +131 -53
- package/dist/generate.js.map +1 -1
- package/dist/graphql-fetcher.d.ts +24 -2
- package/dist/graphql-fetcher.js +112 -24
- package/dist/graphql-fetcher.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/openapi/document.d.ts +15 -0
- package/dist/openapi/document.js +113 -0
- package/dist/openapi/document.js.map +1 -0
- package/dist/openapi/generate.d.ts +3 -0
- package/dist/openapi/generate.js +65 -0
- package/dist/openapi/generate.js.map +1 -0
- package/dist/openapi/names.d.ts +6 -0
- package/dist/openapi/names.js +24 -0
- package/dist/openapi/names.js.map +1 -0
- package/dist/openapi/operations.d.ts +10 -0
- package/dist/openapi/operations.js +319 -0
- package/dist/openapi/operations.js.map +1 -0
- package/dist/openapi/schema.d.ts +16 -0
- package/dist/openapi/schema.js +242 -0
- package/dist/openapi/schema.js.map +1 -0
- package/dist/openapi-fetcher.d.ts +3 -14
- package/dist/openapi-fetcher.js +2 -35
- package/dist/openapi-fetcher.js.map +1 -1
- package/dist/openapi-runtime.d.ts +45 -0
- package/dist/openapi-runtime.js +291 -0
- package/dist/openapi-runtime.js.map +1 -0
- package/dist/openapi-runtime.source +302 -0
- package/dist/path-utils.d.ts +2 -0
- package/dist/path-utils.js +5 -0
- package/dist/path-utils.js.map +1 -0
- package/dist/types.d.ts +7 -0
- package/package.json +4 -5
- package/templates/graphql.template +5 -8
- package/templates/openapi.template +0 -45
package/dist/graphql-fetcher.js
CHANGED
|
@@ -1,32 +1,120 @@
|
|
|
1
|
-
function
|
|
2
|
-
|
|
1
|
+
function readConfiguredToken(auth) {
|
|
2
|
+
if (auth.storage === 'none')
|
|
3
|
+
return '';
|
|
4
|
+
if (auth.storage === 'cookie') {
|
|
5
|
+
if (typeof document === 'undefined' || !auth.key)
|
|
6
|
+
return '';
|
|
7
|
+
const cookie = document.cookie
|
|
8
|
+
.split(';')
|
|
9
|
+
.map((part) => part.trim())
|
|
10
|
+
.find((part) => part.startsWith(`${auth.key}=`));
|
|
11
|
+
if (!cookie)
|
|
12
|
+
return '';
|
|
13
|
+
const value = cookie.slice(auth.key.length + 1);
|
|
14
|
+
try {
|
|
15
|
+
return decodeURIComponent(value);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (typeof globalThis[auth.storage] === 'undefined' || !auth.key)
|
|
22
|
+
return '';
|
|
23
|
+
return globalThis[auth.storage].getItem(auth.key) ?? '';
|
|
24
|
+
}
|
|
25
|
+
function authorizationValue(configuration) {
|
|
26
|
+
if (configuration.getToken)
|
|
27
|
+
return configuration.getToken() ?? '';
|
|
28
|
+
const auth = configuration.auth;
|
|
29
|
+
if (!auth)
|
|
30
|
+
return '';
|
|
31
|
+
const token = readConfiguredToken(auth);
|
|
32
|
+
if (!token || auth.scheme === false)
|
|
33
|
+
return token;
|
|
34
|
+
const scheme = auth.scheme ?? 'Bearer';
|
|
35
|
+
if (!scheme)
|
|
36
|
+
return token;
|
|
37
|
+
return token.toLowerCase().startsWith(`${scheme.toLowerCase()} `) ? token : `${scheme} ${token}`;
|
|
38
|
+
}
|
|
39
|
+
export class GraphqlHttpError extends Error {
|
|
40
|
+
status;
|
|
41
|
+
statusText;
|
|
42
|
+
endpoint;
|
|
43
|
+
body;
|
|
44
|
+
constructor(status, statusText, endpoint, body) {
|
|
45
|
+
super(`GraphQL request to ${endpoint} failed with HTTP ${status}${statusText ? ` ${statusText}` : ''}.`);
|
|
46
|
+
this.status = status;
|
|
47
|
+
this.statusText = statusText;
|
|
48
|
+
this.endpoint = endpoint;
|
|
49
|
+
this.body = body;
|
|
50
|
+
this.name = 'GraphqlHttpError';
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export class GraphqlResponseError extends Error {
|
|
54
|
+
errors;
|
|
55
|
+
data;
|
|
56
|
+
constructor(errors, data) {
|
|
57
|
+
super(errors
|
|
58
|
+
.map(({ message }) => message)
|
|
59
|
+
.filter(Boolean)
|
|
60
|
+
.join('\n') || 'GraphQL response contains errors.');
|
|
61
|
+
this.errors = errors;
|
|
62
|
+
this.data = data;
|
|
63
|
+
this.name = 'GraphqlResponseError';
|
|
64
|
+
}
|
|
3
65
|
}
|
|
4
66
|
export function createGraphqlFetcher(configuration = {}) {
|
|
5
67
|
const endpoint = configuration.endpoint ?? '/api/graphql';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
68
|
+
return function graphqlFetcher(query, variables) {
|
|
69
|
+
return async (context) => {
|
|
70
|
+
try {
|
|
71
|
+
const headers = new Headers({ 'content-type': 'application/json' });
|
|
72
|
+
const token = authorizationValue(configuration);
|
|
73
|
+
if (token)
|
|
74
|
+
headers.set('authorization', token);
|
|
75
|
+
new Headers(configuration.headers).forEach((value, key) => {
|
|
76
|
+
headers.set(key, value);
|
|
77
|
+
});
|
|
78
|
+
let response;
|
|
79
|
+
try {
|
|
80
|
+
response = await fetch(endpoint, {
|
|
81
|
+
body: JSON.stringify({ query, variables }),
|
|
82
|
+
headers,
|
|
83
|
+
method: 'POST',
|
|
84
|
+
signal: context?.signal,
|
|
85
|
+
credentials: configuration.auth?.credentials,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
if (error instanceof Error && error.name === 'AbortError')
|
|
90
|
+
throw error;
|
|
91
|
+
throw new Error(`Could not reach GraphQL endpoint ${endpoint}: ${error instanceof Error ? error.message : String(error)}`, {
|
|
92
|
+
cause: error,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
let result;
|
|
96
|
+
const rawBody = await response.text();
|
|
97
|
+
try {
|
|
98
|
+
result = JSON.parse(rawBody);
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
if (!response.ok)
|
|
102
|
+
throw new GraphqlHttpError(response.status, response.statusText, endpoint, rawBody);
|
|
103
|
+
throw new Error(`GraphQL server returned an invalid JSON response from ${endpoint}.`, { cause: error });
|
|
104
|
+
}
|
|
105
|
+
if (!response.ok)
|
|
106
|
+
throw new GraphqlHttpError(response.status, response.statusText, endpoint, result);
|
|
107
|
+
if (result.errors?.length)
|
|
108
|
+
throw new GraphqlResponseError(result.errors, result.data);
|
|
109
|
+
if (result.data === undefined) {
|
|
110
|
+
throw new Error(`GraphQL response from ${endpoint} does not contain data.`);
|
|
111
|
+
}
|
|
112
|
+
return result.data;
|
|
21
113
|
}
|
|
22
|
-
|
|
23
|
-
throw
|
|
114
|
+
catch (error) {
|
|
115
|
+
throw error instanceof Error ? error : new Error(String(error));
|
|
24
116
|
}
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
catch (error) {
|
|
28
|
-
throw error instanceof Error ? error : new Error(String(error));
|
|
29
|
-
}
|
|
117
|
+
};
|
|
30
118
|
};
|
|
31
119
|
}
|
|
32
120
|
export const graphqlFetcher = createGraphqlFetcher();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graphql-fetcher.js","sourceRoot":"","sources":["../src/graphql-fetcher.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"graphql-fetcher.js","sourceRoot":"","sources":["../src/graphql-fetcher.ts"],"names":[],"mappings":"AAaA,SAAS,mBAAmB,CAAC,IAAgB;IAC3C,IAAI,IAAI,CAAC,OAAO,KAAK,MAAM;QAAE,OAAO,EAAE,CAAC;IACvC,IA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c,GAAG,oBAAoB,EAAE,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { generate } from './generate.js';
|
|
2
2
|
export type { GraphqlFetcherConfiguration } from './graphql-fetcher.js';
|
|
3
|
-
export { createGraphqlFetcher, graphqlFetcher } from './graphql-fetcher.js';
|
|
3
|
+
export { createGraphqlFetcher, GraphqlHttpError, GraphqlResponseError, graphqlFetcher } from './graphql-fetcher.js';
|
|
4
4
|
export type { OpenapiFetcherConfiguration, OpenapiFetcherOptions } from './openapi-fetcher.js';
|
|
5
|
-
export { createOpenapiFetcher, openapiFetcher } from './openapi-fetcher.js';
|
|
6
|
-
export type { CodegenConfig, CodegenResult } from './types.js';
|
|
5
|
+
export { createOpenapiFetcher, OpenapiHttpError, openapiFetcher } from './openapi-fetcher.js';
|
|
6
|
+
export type { AuthConfig, CodegenConfig, CodegenResult, TokenStorage } from './types.js';
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { generate } from './generate.js';
|
|
2
|
-
export { createGraphqlFetcher, graphqlFetcher } from './graphql-fetcher.js';
|
|
3
|
-
export { createOpenapiFetcher, openapiFetcher } from './openapi-fetcher.js';
|
|
2
|
+
export { createGraphqlFetcher, GraphqlHttpError, GraphqlResponseError, graphqlFetcher } from './graphql-fetcher.js';
|
|
3
|
+
export { createOpenapiFetcher, OpenapiHttpError, openapiFetcher } from './openapi-fetcher.js';
|
|
4
4
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC,OAAO,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAEpH,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type ObjectValue = Record<string, unknown>;
|
|
2
|
+
export declare function object(value: unknown, location: string): ObjectValue;
|
|
3
|
+
export declare function list(value: unknown, location: string): unknown[];
|
|
4
|
+
export declare function string(value: unknown, location: string): string;
|
|
5
|
+
export declare function pointer(...parts: string[]): string;
|
|
6
|
+
export declare class OpenapiDocument {
|
|
7
|
+
readonly root: ObjectValue;
|
|
8
|
+
readonly version: '3.0' | '3.1';
|
|
9
|
+
readonly schemas: ObjectValue;
|
|
10
|
+
constructor(root: ObjectValue);
|
|
11
|
+
reference(ref: string, location: string): unknown;
|
|
12
|
+
canonicalReference(ref: string, location: string): string;
|
|
13
|
+
resolve(value: unknown, location: string, visited?: Set<string>): ObjectValue;
|
|
14
|
+
}
|
|
15
|
+
export declare function loadOpenapi(source: string): Promise<OpenapiDocument>;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { parseDocument } from 'yaml';
|
|
3
|
+
export function object(value, location) {
|
|
4
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
5
|
+
throw new Error(`${location} must be an object.`);
|
|
6
|
+
}
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
9
|
+
export function list(value, location) {
|
|
10
|
+
if (!Array.isArray(value))
|
|
11
|
+
throw new Error(`${location} must be an array.`);
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
export function string(value, location) {
|
|
15
|
+
if (typeof value !== 'string' || !value.trim())
|
|
16
|
+
throw new Error(`${location} must be a non-empty string.`);
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
export function pointer(...parts) {
|
|
20
|
+
return `#/${parts.map((part) => part.replaceAll('~', '~0').replaceAll('/', '~1')).join('/')}`;
|
|
21
|
+
}
|
|
22
|
+
export class OpenapiDocument {
|
|
23
|
+
root;
|
|
24
|
+
version;
|
|
25
|
+
schemas;
|
|
26
|
+
constructor(root) {
|
|
27
|
+
this.root = root;
|
|
28
|
+
if (root.swagger !== undefined)
|
|
29
|
+
throw new Error('Swagger 2.0 is not supported. Use OpenAPI 3.0 or 3.1.');
|
|
30
|
+
const version = string(root.openapi, '#/openapi');
|
|
31
|
+
if (!/^3\.(0|1)\.\d+$/.test(version))
|
|
32
|
+
throw new Error(`Unsupported OpenAPI version "${version}". Use 3.0.x or 3.1.x.`);
|
|
33
|
+
this.version = version.startsWith('3.0.') ? '3.0' : '3.1';
|
|
34
|
+
const info = object(root.info, '#/info');
|
|
35
|
+
string(info.title, '#/info/title');
|
|
36
|
+
string(info.version, '#/info/version');
|
|
37
|
+
const components = root.components === undefined ? {} : object(root.components, '#/components');
|
|
38
|
+
this.schemas = components.schemas === undefined ? {} : object(components.schemas, '#/components/schemas');
|
|
39
|
+
if (root.webhooks !== undefined && Object.keys(object(root.webhooks, '#/webhooks')).length) {
|
|
40
|
+
throw new Error('#/webhooks is not supported by the HTTP client generator.');
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
reference(ref, location) {
|
|
44
|
+
if (!ref.startsWith('#/'))
|
|
45
|
+
throw new Error(`${location}: external $ref "${ref}" is not supported; bundle the schema first.`);
|
|
46
|
+
let current = this.root;
|
|
47
|
+
let parts;
|
|
48
|
+
try {
|
|
49
|
+
parts = decodeURIComponent(ref.slice(2))
|
|
50
|
+
.split('/')
|
|
51
|
+
.map((part) => part.replaceAll('~1', '/').replaceAll('~0', '~'));
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
throw new Error(`${location}: invalid reference "${ref}".`);
|
|
55
|
+
}
|
|
56
|
+
for (const part of parts) {
|
|
57
|
+
if (current === null || typeof current !== 'object' || !Object.hasOwn(current, part))
|
|
58
|
+
throw new Error(`${location}: unresolved $ref "${ref}".`);
|
|
59
|
+
current = current[part];
|
|
60
|
+
}
|
|
61
|
+
return current;
|
|
62
|
+
}
|
|
63
|
+
canonicalReference(ref, location) {
|
|
64
|
+
this.reference(ref, location);
|
|
65
|
+
return pointer(...decodeURIComponent(ref.slice(2))
|
|
66
|
+
.split('/')
|
|
67
|
+
.map((part) => part.replaceAll('~1', '/').replaceAll('~0', '~')));
|
|
68
|
+
}
|
|
69
|
+
resolve(value, location, visited = new Set()) {
|
|
70
|
+
const record = object(value, location);
|
|
71
|
+
if (record.$ref === undefined)
|
|
72
|
+
return record;
|
|
73
|
+
const ref = string(record.$ref, `${location}/$ref`);
|
|
74
|
+
if (visited.has(ref))
|
|
75
|
+
throw new Error(`${location}: circular reference "${ref}".`);
|
|
76
|
+
visited.add(ref);
|
|
77
|
+
return this.resolve(this.reference(ref, location), ref, visited);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
export async function loadOpenapi(source) {
|
|
81
|
+
let content;
|
|
82
|
+
if (/^https?:\/\//i.test(source)) {
|
|
83
|
+
const response = await fetch(source, { signal: AbortSignal.timeout(30_000) });
|
|
84
|
+
if (!response.ok)
|
|
85
|
+
throw new Error(`Could not load ${source}: HTTP ${response.status}.`);
|
|
86
|
+
content = await response.text();
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
content = await readFile(source, 'utf8');
|
|
90
|
+
}
|
|
91
|
+
const parsed = parseDocument(content, { uniqueKeys: true });
|
|
92
|
+
if (parsed.errors.length)
|
|
93
|
+
throw new Error(`Invalid YAML/JSON in ${source}: ${parsed.errors.map((error) => error.message).join('\n')}`);
|
|
94
|
+
const root = object(parsed.toJS({ maxAliasCount: 100 }), source);
|
|
95
|
+
const visited = new Set();
|
|
96
|
+
const active = new Set();
|
|
97
|
+
function check(value) {
|
|
98
|
+
if (!value || typeof value !== 'object')
|
|
99
|
+
return;
|
|
100
|
+
if (active.has(value))
|
|
101
|
+
throw new Error('Cyclic YAML aliases are not supported. Use OpenAPI $ref for recursive schemas.');
|
|
102
|
+
if (visited.has(value))
|
|
103
|
+
return;
|
|
104
|
+
active.add(value);
|
|
105
|
+
for (const child of Object.values(value))
|
|
106
|
+
check(child);
|
|
107
|
+
active.delete(value);
|
|
108
|
+
visited.add(value);
|
|
109
|
+
}
|
|
110
|
+
check(root);
|
|
111
|
+
return new OpenapiDocument(root);
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=document.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"document.js","sourceRoot":"","sources":["../../src/openapi/document.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,MAAM,CAAC;AAIrC,MAAM,UAAU,MAAM,CAAC,KAAc,EAAE,QAAgB;IACrD,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QACxE,MAAM,IAAI,KAAK,CAAC,GAAG,QAAQ,qBAAqB,CAAC,CAAC;IACpD,CAAC;IACD,OAAO,KAAoB,CAAC;AAC9B,CAAC;AAED,MAAM,UAAU,IAAI,CAAC,KAAc,EAAE,QAAgB;IACnD,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,GAAG,QAAQ,oBAAoB,CAAC,CAAC;IAC5E,OAAO,KAAK,CAAC;AACf,CAAC;AAED,MAAM,UAAU,MAAM,CAAC,KAAc,EAAE,QAAgB;IACrD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,GAAG,QAAQ,8BAA8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KAAK,CAAC,CAAC;QAClB,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC;YAAE,KAAK,CAAC,KAAK,CAAC,CAAC;QACvD,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACrB,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACrB,CAAC;IACD,KAAK,CAAC,IAAI,CAAC,CAAC;IACZ,OAAO,IAAI,eAAe,CAAC,IAAI,CAAC,CAAC;AACnC,CAAC"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { OpenapiFetcherConfiguration } from '../openapi-runtime.js';
|
|
2
|
+
/** Build the complete client in memory. The runtime is copied verbatim from checked TypeScript. */
|
|
3
|
+
export declare function generateOpenapiSource(source: string, configuration: OpenapiFetcherConfiguration): Promise<string>;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { loadOpenapi } from './document.js';
|
|
3
|
+
import { Names } from './names.js';
|
|
4
|
+
import { Operations } from './operations.js';
|
|
5
|
+
import { Schemas } from './schema.js';
|
|
6
|
+
/** Build the complete client in memory. The runtime is copied verbatim from checked TypeScript. */
|
|
7
|
+
export async function generateOpenapiSource(source, configuration) {
|
|
8
|
+
const document = await loadOpenapi(source);
|
|
9
|
+
const runtime = await readFile(new URL('../openapi-runtime.source', import.meta.url), 'utf8');
|
|
10
|
+
const names = new Names();
|
|
11
|
+
for (const name of [
|
|
12
|
+
'reactQuery',
|
|
13
|
+
'TokenStorage',
|
|
14
|
+
'AuthConfig',
|
|
15
|
+
'OpenapiFetcherOptions',
|
|
16
|
+
'OpenapiFetcherConfiguration',
|
|
17
|
+
'OpenapiParameter',
|
|
18
|
+
'OpenapiHttpError',
|
|
19
|
+
'createOpenapiFetcher',
|
|
20
|
+
'openapiFetcher',
|
|
21
|
+
'readConfiguredToken',
|
|
22
|
+
'authorizationValue',
|
|
23
|
+
'withBaseUrl',
|
|
24
|
+
'parameterScalar',
|
|
25
|
+
'parameterParts',
|
|
26
|
+
'serializeQueryParams',
|
|
27
|
+
'requestBody',
|
|
28
|
+
'requestHeaders',
|
|
29
|
+
'withQueryParams',
|
|
30
|
+
'readResponse',
|
|
31
|
+
'Array',
|
|
32
|
+
'Record',
|
|
33
|
+
'Promise',
|
|
34
|
+
'Omit',
|
|
35
|
+
'TypeError',
|
|
36
|
+
'Headers',
|
|
37
|
+
'HeadersInit',
|
|
38
|
+
'RequestCredentials',
|
|
39
|
+
'BodyInit',
|
|
40
|
+
'AbortSignal',
|
|
41
|
+
'Response',
|
|
42
|
+
'Blob',
|
|
43
|
+
'FormData',
|
|
44
|
+
'URLSearchParams',
|
|
45
|
+
'Object',
|
|
46
|
+
'String',
|
|
47
|
+
'JSON',
|
|
48
|
+
'globalThis',
|
|
49
|
+
'document',
|
|
50
|
+
'fetch',
|
|
51
|
+
])
|
|
52
|
+
names.reserve(name, 'generated runtime');
|
|
53
|
+
const schemas = new Schemas(document, names);
|
|
54
|
+
const declarations = schemas.declarations();
|
|
55
|
+
const operations = new Operations(schemas).render();
|
|
56
|
+
return [
|
|
57
|
+
'// Generated by @kollors/react-codegen. Do not edit.',
|
|
58
|
+
...(operations ? ["import * as reactQuery from '@tanstack/react-query';"] : []),
|
|
59
|
+
declarations,
|
|
60
|
+
...(operations ? [runtime, `export const openapiFetcher = createOpenapiFetcher(${JSON.stringify(configuration)});`] : []),
|
|
61
|
+
operations,
|
|
62
|
+
'',
|
|
63
|
+
].join('\n\n');
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=generate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generate.js","sourceRoot":"","sources":["../../src/openapi/generate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,KAAK,EAAE,MAA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C;IACpD,OAAO;QACL,sDAAsD;QACtD,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,sDAAsD,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC/E,YAAY;QACZ,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,sDAAsD,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QACzH,UAAU;QACV,EAAE;KACH,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export function identifier(value, upper = false) {
|
|
2
|
+
const name = (value.match(/[A-Za-z0-9$]+/g) ?? []).map((part, index) => (index ? part[0]?.toUpperCase() + part.slice(1) : part)).join('');
|
|
3
|
+
if (!name)
|
|
4
|
+
throw new Error(`Cannot derive a TypeScript identifier from "${value}".`);
|
|
5
|
+
const first = upper ? name[0]?.toUpperCase() : name[0]?.toLowerCase();
|
|
6
|
+
const result = first + name.slice(1);
|
|
7
|
+
return /^\d/.test(result) ? `_${result}` : result;
|
|
8
|
+
}
|
|
9
|
+
export class Names {
|
|
10
|
+
used = new Map();
|
|
11
|
+
reserve(name, location) {
|
|
12
|
+
const previous = this.used.get(name);
|
|
13
|
+
if (previous !== undefined)
|
|
14
|
+
throw new Error(`Generated name "${name}" collides between ${previous} and ${location}. Rename the schema or operationId.`);
|
|
15
|
+
this.used.set(name, location);
|
|
16
|
+
return name;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function comment(value) {
|
|
20
|
+
if (typeof value !== 'string' || !value.trim())
|
|
21
|
+
return '';
|
|
22
|
+
return `/** ${value.replaceAll('*/', '* /').replace(/\r?\n/g, '\n * ')} */\n`;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=names.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"names.js","sourceRoot":"","sources":["../../src/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