@team-supercharge/oasg 13.0.1 → 13.1.0-master-ac68fc1d.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@team-supercharge/oasg",
3
- "version": "13.0.1",
3
+ "version": "13.1.0-master-ac68fc1d.0",
4
4
  "description": "Node-based tool to lint OpenAPI documents and generate clients, servers and documentation from them",
5
5
  "author": "Supercharge",
6
6
  "license": "MIT",
@@ -10,5 +10,15 @@
10
10
  "ARRAY_ITEM_SUFFIX": "",
11
11
  "MAP_ITEM_SUFFIX": "",
12
12
  "SKIP_SCHEMA_REUSE": "true"
13
+ },
14
+ "files": {
15
+ "hook.mustache": {
16
+ "destinationFilename": ".hook.ts",
17
+ "templateType": "API"
18
+ },
19
+ "use-api.hook.mustache": {
20
+ "folder": "src/apis",
21
+ "destinationFilename": "use-api.hook.tsx"
22
+ }
13
23
  }
14
24
  }
@@ -0,0 +1,18 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ {{#useSagaAndRecords}}
4
+ export * from './SagaApiManager{{importFileExtension}}'
5
+ export * from './allSagas{{importFileExtension}}'
6
+ {{/useSagaAndRecords}}
7
+ export * from './use-api.hook{{importFileExtension}}';
8
+ {{#apiInfo}}
9
+ {{#apis}}
10
+ {{#operations}}
11
+ export * from './{{ classFilename }}{{importFileExtension}}';
12
+ export * from './{{ classFilename }}.hook{{importFileExtension}}';
13
+ {{#useSagaAndRecords}}
14
+ export * from './{{{ classFilename }}}Sagas{{importFileExtension}}';
15
+ {{/useSagaAndRecords}}
16
+ {{/operations}}
17
+ {{/apis}}
18
+ {{/apiInfo}}
@@ -0,0 +1,111 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ {{>licenseInfo}}
4
+
5
+ import { useMutation, UseMutationOptions, UseMutationResult, useQuery, UseQueryOptions, UseQueryResult } from '@tanstack/react-query';
6
+ import { useApi } from './use-api.hook';
7
+ {{#imports.0}}
8
+ import type {
9
+ {{#imports}}
10
+ {{className}},
11
+ {{/imports}}
12
+ } from '../models/index{{importFileExtension}}';
13
+ {{/imports.0}}
14
+ {{#operations}}
15
+ import {
16
+ {{classname}},
17
+ {{#operation}}
18
+ {{#allParams.0}}
19
+ {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request,
20
+ {{/allParams.0}}
21
+ {{/operation}}
22
+ } from './{{ classFilename }}{{importFileExtension}}';
23
+ {{/operations}}
24
+
25
+ {{#operations}}
26
+ {{#operation}}
27
+
28
+ export type {{classname}}{{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Parameters =
29
+ {{^useSingleRequestParameter}}
30
+ { parameters: Parameters<{{classname}}['{{nickname}}']>};
31
+ {{/useSingleRequestParameter}}
32
+ {{#useSingleRequestParameter}}
33
+ {{#allParams.0}}{{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request{{/allParams.0}}{{^allParams.0}}void{{/allParams.0}};
34
+ {{/useSingleRequestParameter}}
35
+
36
+ /**
37
+ {{#notes}}
38
+ * {{&notes}}
39
+ {{/notes}}
40
+ {{#summary}}
41
+ * {{&summary}}
42
+ {{/summary}}
43
+ {{#isDeprecated}}
44
+ * @deprecated
45
+ {{/isDeprecated}}
46
+ */
47
+ export const use{{classname}}{{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Mutation = <TContext = unknown>(
48
+ { onSuccess, ...options }: Omit<UseMutationOptions<{{{returnType}}}{{^returnType}}void{{/returnType}}, Error, {{classname}}{{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Parameters, TContext>, 'mutationFn'> = {}
49
+ ): UseMutationResult<{{{returnType}}}{{^returnType}}void{{/returnType}}, Error, {{classname}}{{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Parameters, TContext> => {
50
+ const api = useApi({{classname}});
51
+ return useMutation<{{{returnType}}}{{^returnType}}void{{/returnType}}, Error, {{classname}}{{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Parameters, TContext>({
52
+ {{^useSingleRequestParameter}}
53
+ mutationFn: ({ parameters }) => api.{{nickname}}(...parameters),
54
+ {{/useSingleRequestParameter}}
55
+ {{#useSingleRequestParameter}}
56
+ {{#allParams.0}}
57
+ mutationFn: (parameters) => api.{{nickname}}(parameters),
58
+ {{/allParams.0}}
59
+ {{^allParams.0}}
60
+ mutationFn: () => api.{{nickname}}(),
61
+ {{/allParams.0}}
62
+ {{/useSingleRequestParameter}}
63
+ ...options,
64
+ });
65
+ }
66
+ /**
67
+ {{#notes}}
68
+ * {{&notes}}
69
+ {{/notes}}
70
+ {{#summary}}
71
+ * {{&summary}}
72
+ {{/summary}}
73
+ {{#isDeprecated}}
74
+ * @deprecated
75
+ {{/isDeprecated}}
76
+ */
77
+ export const use{{classname}}{{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Query = (
78
+ options: Partial<Omit<UseQueryOptions<{{{returnType}}}{{^returnType}}void{{/returnType}}>, 'queryFn'>> = {}
79
+ {{^useSingleRequestParameter}}
80
+ ,{parameters}: Partial<{{classname}}{{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Parameters>
81
+ {{/useSingleRequestParameter}}
82
+ {{#useSingleRequestParameter}}
83
+ {{#allParams.0}}
84
+ ,parameter: {{classname}}{{#lambda.titlecase}}{{nickname}}{{/lambda.titlecase}}Parameters | undefined
85
+ {{/allParams.0}}
86
+ {{/useSingleRequestParameter}}
87
+ ): UseQueryResult<{{{returnType}}}{{^returnType}}void{{/returnType}}> => {
88
+ const api = useApi({{classname}});
89
+ return useQuery<{{{returnType}}}{{^returnType}}void{{/returnType}}>({
90
+ {{^useSingleRequestParameter}}
91
+ queryKey: ['{{classname}}','{{nickname}}', ...requestParameters],
92
+ queryFn: ({ signal }) => api.{{nickname}}(...requestParameters.slice(0, -1), { ...requestParameters[requestParameters.length - 1], signal }),
93
+ enabled: !!parameters,
94
+ {{/useSingleRequestParameter}}
95
+ {{#useSingleRequestParameter}}
96
+ {{#allParams.0}}
97
+ queryKey: ['{{classname}}','{{nickname}}', parameter],
98
+ queryFn: ({ signal }) => api.{{nickname}}(parameter, { signal }),
99
+ enabled: !!parameter,
100
+ {{/allParams.0}}
101
+ {{^allParams.0}}
102
+ queryKey: ['{{classname}}','{{nickname}}'],
103
+ queryFn: ({ signal }) => api.{{nickname}}({ signal }),
104
+ {{/allParams.0}}
105
+ {{/useSingleRequestParameter}}
106
+ ...options,
107
+ });
108
+ }
109
+ {{/operation}}
110
+
111
+ {{/operations}}
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "{{npmName}}",
3
+ "version": "{{npmVersion}}",
4
+ "description": "OpenAPI client for {{npmName}}",
5
+ "author": "OpenAPI-Generator",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://{{gitHost}}/{{gitUserId}}/{{gitRepoId}}.git"
9
+ },
10
+ {{#packageAsSourceOnlyLibrary}}
11
+ "main": "./index.ts",
12
+ {{/packageAsSourceOnlyLibrary}}
13
+ {{^packageAsSourceOnlyLibrary}}
14
+ "main": "./dist/index.js",
15
+ "typings": "./dist/index.d.ts",
16
+ {{#supportsES6}}
17
+ "module": "./dist/esm/index.js",
18
+ "sideEffects": false,
19
+ {{/supportsES6}}
20
+ "scripts": {
21
+ "build": "tsc{{#supportsES6}} && tsc -p tsconfig.esm.json{{/supportsES6}}"{{^sagasAndRecords}},
22
+ "prepare": "npm run build"{{/sagasAndRecords}}
23
+ },
24
+ {{/packageAsSourceOnlyLibrary}}
25
+ "devDependencies": {
26
+ {{#sagasAndRecords}}
27
+ "immutable": "^4.0.0-rc.12",
28
+ "normalizr": "^3.6.1",
29
+ "redux-saga": "^1.1.3",
30
+ "redux-ts-simple": "^3.2.0",
31
+ "reselect": "^4.0.0",
32
+ {{/sagasAndRecords}}
33
+ "typescript": "^4.0",
34
+ "@types/react": "^18.2.24"
35
+ },
36
+ "peerDependencies": {
37
+ "react": "^18.2.0",
38
+ "@tanstack/react-query": "^5.12.2"
39
+ }{{#npmRepository}},{{/npmRepository}}
40
+ {{#npmRepository}}
41
+ "publishConfig": {
42
+ "registry": "{{npmRepository}}"
43
+ }
44
+ {{/npmRepository}}
45
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "module": "esnext",
5
+ "outDir": "dist/esm",
6
+ "jsx": "react-jsx"
7
+ }
8
+ }
@@ -0,0 +1,26 @@
1
+ {
2
+ "compilerOptions": {
3
+ "declaration": true,
4
+ "target": "{{#supportsES6}}es6{{/supportsES6}}{{^supportsES6}}es5{{/supportsES6}}",
5
+ {{#sagasAndRecords}}
6
+ "strict": true,
7
+ {{/sagasAndRecords}}
8
+ "module": "commonjs",
9
+ "moduleResolution": "node",
10
+ "outDir": "dist",
11
+ {{^supportsES6}}
12
+ "lib": [
13
+ "es6",
14
+ "dom"
15
+ ],
16
+ {{/supportsES6}}
17
+ "typeRoots": [
18
+ "node_modules/@types"
19
+ ],
20
+ "jsx": "react-jsx",
21
+ },
22
+ "exclude": [
23
+ "dist",
24
+ "node_modules"
25
+ ]
26
+ }
@@ -0,0 +1,155 @@
1
+ import {
2
+ createContext,
3
+ useContext,
4
+ useCallback,
5
+ useMemo,
6
+ FC,
7
+ PropsWithChildren,
8
+ } from "react";
9
+ import * as runtime from '../runtime';
10
+ {{#apiInfo}}
11
+ {{#apis}}
12
+ {{#operations}}
13
+ import { {{classname}} } from './{{ classFilename }}{{importFileExtension}}';
14
+ {{/operations}}
15
+ {{/apis}}
16
+
17
+
18
+ type APIS_IN_ORDER = [
19
+ {{#apis}}
20
+ {{#operations}}
21
+ {{classname}},
22
+ {{/operations}}
23
+ {{/apis}}
24
+ ];
25
+
26
+ export type APIS = APIS_IN_ORDER[number];
27
+
28
+ export declare interface Type<T> extends Function {
29
+ new (...args: any[]): T;
30
+ }
31
+
32
+ export const ApiContext = createContext<{ getApi: <T extends APIS>(api: Type<T>) => T | undefined }>({
33
+ getApi: () => undefined,
34
+ });
35
+
36
+ export const getApiInstance = <TArray extends APIS[], T extends TArray[number]>(
37
+ api: Type<T>,
38
+ apis: TArray
39
+ ): T => {
40
+ return apis.find((a) => a instanceof api) as T;
41
+ };
42
+
43
+ export const ApiProvider: FC<PropsWithChildren<{ apis: APIS[] }>> = ({
44
+ apis,
45
+ children,
46
+ }) => {
47
+ const parent = useContext(ApiContext);
48
+ const getApi = useCallback(<T extends APIS>(api: Type<T>): T => {
49
+ const apiInstance = (apis.find((a) => a instanceof api) ?? parent.getApi(api)) as T | undefined;
50
+ if (!apiInstance) {
51
+ throw new Error(`API instance for ${api.name} not found. Please provide it using ApiProvider from '{{npmName}}'`);
52
+ }
53
+ return apiInstance;
54
+ }, [apis, parent.getApi]);
55
+ const value = useMemo(() => ({ getApi }), [getApi]);
56
+
57
+ return (
58
+ <ApiContext.Provider value={value}>
59
+ {children}
60
+ </ApiContext.Provider>
61
+ );
62
+ };
63
+
64
+ export const useApi = <T extends APIS>(api: Type<T>): T => {
65
+ const { getApi } = useContext(ApiContext);
66
+ return getApi(api);
67
+ }
68
+
69
+ export const ApiConfigContext = createContext<runtime.ConfigurationParameters>({});
70
+
71
+ export const ApiConfigProvider: FC<PropsWithChildren<{ config: runtime.ConfigurationParameters }>> = ({
72
+ config,
73
+ children,
74
+ }) => {
75
+ return (
76
+ <ApiConfigContext.Provider value={config}>
77
+ {children}
78
+ </ApiConfigContext.Provider>
79
+ );
80
+ };
81
+
82
+ export const ApiConfigWithInheritProvider: FC<PropsWithChildren<{ config: runtime.ConfigurationParameters }>> = ({
83
+ config,
84
+ children,
85
+ }) => {
86
+ const parent = useContext(ApiConfigContext);
87
+ const value = useMemo(() => ({
88
+ ...parent,
89
+ ...config,
90
+ middleware: [...parent.middleware ?? [], ...config.middleware ?? []],
91
+ headers: { ...parent.headers, ...config.headers },
92
+ }), [parent, config]);
93
+
94
+ return (
95
+ <ApiConfigContext.Provider value={value}>
96
+ {children}
97
+ </ApiConfigContext.Provider>
98
+ );
99
+ };
100
+
101
+ export const useApiConfig = (): runtime.Configuration => {
102
+ const configParameters = useContext(ApiConfigContext);
103
+ const config = useMemo(() => new runtime.Configuration(configParameters), [configParameters]);
104
+ return config;
105
+ }
106
+
107
+ type ReadonlyApiConstructorToType<T> = T extends [
108
+ infer ARRAYFIRST,
109
+ ...infer ARRAYREST
110
+ ]
111
+ ? ARRAYFIRST extends InstanceType<infer ARRAYFIRSTTYPE>
112
+ ? [InstanceType<ARRAYFIRST>, ...ReadonlyApiConstructorToType<ARRAYREST>]
113
+ : []
114
+ : [];
115
+
116
+ type ApiConstructorToType<T extends Array<unknown>> = T[0] extends undefined
117
+ ? APIS_IN_ORDER
118
+ : ReadonlyApiConstructorToType<T> extends []
119
+ ? APIS[]
120
+ : ReadonlyApiConstructorToType<T>;
121
+
122
+ export const createApiInstances = <T extends Type<APIS>[]>(
123
+ config: runtime.Configuration,
124
+ apis: T = [] as unknown as T
125
+ ): ApiConstructorToType<T> => {
126
+ if (apis.length > 0) {
127
+ return apis.map((api) => new api(config)) as ApiConstructorToType<T>;
128
+ }
129
+ return [
130
+ {{#apis}}
131
+ {{#operations}}
132
+ new {{classname}}(config),
133
+ {{/operations}}
134
+ {{/apis}}
135
+ ] as unknown as ApiConstructorToType<T>;
136
+ };
137
+
138
+ export const useCreateApiInstances = <T extends Type<APIS>[]>(
139
+ apis: T = [] as unknown as T
140
+ ): ApiConstructorToType<T> => {
141
+ const config = useApiConfig();
142
+ return useMemo(() => createApiInstances(config, apis), [apis, config]);
143
+ };
144
+ {{/apiInfo}}
145
+
146
+ export const ApiInstancesProvider: FC<PropsWithChildren<{ apis: Type<APIS>[] }>> = (
147
+ { apis, children }
148
+ ) => {
149
+ const apiInstances = useCreateApiInstances(apis);
150
+ return (
151
+ <ApiProvider apis={apiInstances}>
152
+ {children}
153
+ </ApiProvider>
154
+ );
155
+ };