openapi_generate_typescript_fetch 0.1.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.
- checksums.yaml +7 -0
- data/LICENSE.txt +17 -0
- data/lib/openapi_generate_typescript_fetch/schema.rb +115 -0
- data/lib/openapi_generate_typescript_fetch/taskinfo.rb +52 -0
- data/lib/openapi_generate_typescript_fetch/tasks.rb +43 -0
- data/lib/openapi_generate_typescript_fetch/version.rb +9 -0
- data/lib/openapi_generate_typescript_fetch.rb +107 -0
- data/template/package.json.erb +48 -0
- data/template/src/callclasses.ts.erb +542 -0
- data/template/src/helpers.ts.erb +191 -0
- data/template/src/index.ts.erb +19 -0
- data/template/src/schemas.ts.erb +1016 -0
- data/template/src/servers.ts.erb +226 -0
- data/template/src/shared.ts.erb +114 -0
- data/template/test/callclasses.ts.erb +844 -0
- data/template/test/helpers.ts.erb +1214 -0
- data/template/test/makers.ts.erb +1237 -0
- data/template/test/schemas.ts.erb +664 -0
- data/template/test/servers.ts.erb +109 -0
- data/template/test/shared.ts.erb +107 -0
- data/template/tsconfig.json.erb +51 -0
- metadata +112 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
<%=
|
|
2
|
+
Gen.output.config = Gen.x.cfg['ts_indentation']
|
|
3
|
+
Gen.x.generator_info
|
|
4
|
+
%>
|
|
5
|
+
|
|
6
|
+
import type {String2String,CallExtras} from './schemas.d.ts';
|
|
7
|
+
import type {Shared} from './shared.d.ts';
|
|
8
|
+
import { shared } from './shared.js';
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
export interface Bodyful {
|
|
12
|
+
body?: any;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const jsonBodyEncoder = new TextEncoder;
|
|
16
|
+
export function addJsonBody(target: Bodyful, body: any, headers: String2String): void {
|
|
17
|
+
if (!body)
|
|
18
|
+
return;
|
|
19
|
+
const b: Uint8Array = jsonBodyEncoder.encode(JSON.stringify(body));
|
|
20
|
+
target.body = b;
|
|
21
|
+
headers['Content-Type'] = 'application/json';
|
|
22
|
+
headers['Content-Length'] = b.length.toString();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function str2integer(value: string|null): number|null {
|
|
26
|
+
if (value === null)
|
|
27
|
+
return null;
|
|
28
|
+
return parseInt(value);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function str2float(value: string|null): number|null {
|
|
32
|
+
if (value === null)
|
|
33
|
+
return null;
|
|
34
|
+
return parseFloat(value);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface SchemaFunctions {
|
|
38
|
+
is: (x: unknown) => boolean;
|
|
39
|
+
base: (x: any) => any;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface PartialSchemaFunctions {
|
|
43
|
+
is: (x: unknown) => boolean;
|
|
44
|
+
base?: (x: any) => any;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface Name2Functions {
|
|
48
|
+
[key: string]: SchemaFunctions;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface PatternFunctions {
|
|
52
|
+
re: RegExp;
|
|
53
|
+
funcs: SchemaFunctions;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface SchemaCheckers {
|
|
57
|
+
requiredKeys: Array<string>;
|
|
58
|
+
required: Name2Functions;
|
|
59
|
+
optional: Name2Functions;
|
|
60
|
+
patterns: Array<PatternFunctions>;
|
|
61
|
+
additional?: PartialSchemaFunctions;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type String2Any = {
|
|
65
|
+
[key: string]: any;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function isSchema(x: object, checkers: SchemaCheckers): boolean {
|
|
69
|
+
for (const key of checkers.requiredKeys) {
|
|
70
|
+
if (x[key as keyof object] === undefined)
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
const keys = Object.keys(x);
|
|
74
|
+
for (const key of keys) {
|
|
75
|
+
const v = x[key as keyof object];
|
|
76
|
+
if (checkers.required[key] !== undefined) {
|
|
77
|
+
if (!checkers.required[key].is(v))
|
|
78
|
+
return false;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (checkers.optional[key] !== undefined) {
|
|
82
|
+
if (!checkers.optional[key].is(v))
|
|
83
|
+
return false;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (checkers.patterns.length > 0) {
|
|
87
|
+
let patternMatched = false;
|
|
88
|
+
for (const rf of checkers.patterns) {
|
|
89
|
+
if (rf.re.test(key)) {
|
|
90
|
+
patternMatched = true;
|
|
91
|
+
if (!rf.funcs.is(v))
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (patternMatched)
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (checkers.additional && !checkers.additional.is(v)) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function baseSchema(x: object, checkers: SchemaCheckers): String2Any {
|
|
106
|
+
const keys = Object.keys(x);
|
|
107
|
+
const out: String2Any = {};
|
|
108
|
+
for (const key of keys) {
|
|
109
|
+
const v = x[key as keyof object];
|
|
110
|
+
if (checkers.required[key] !== undefined) {
|
|
111
|
+
out[key as keyof String2Any] = checkers.required[key].base(v);
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (checkers.optional[key] !== undefined) {
|
|
115
|
+
out[key as keyof String2Any] = checkers.optional[key].base(v);
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (checkers.patterns.length > 0) {
|
|
119
|
+
let patternMatched = false;
|
|
120
|
+
for (const rf of checkers.patterns) {
|
|
121
|
+
if (rf.re.test(key)) {
|
|
122
|
+
out[key as keyof String2Any] = rf.funcs.base(v);
|
|
123
|
+
patternMatched = true;
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (patternMatched)
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
if (checkers.additional && checkers.additional.base) {
|
|
131
|
+
out[key as keyof String2Any] = checkers.additional.base(v);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return out;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function setExtras(thisExtras: CallExtras|undefined, extras: CallExtras): CallExtras {
|
|
138
|
+
const x: CallExtras = {};
|
|
139
|
+
if (shared.extras?.query || thisExtras?.query || extras.query) {
|
|
140
|
+
x.query = { ...(shared.extras?.query || {}), ...(thisExtras?.query || {}), ...(extras.query || {}) };
|
|
141
|
+
}
|
|
142
|
+
if (shared.extras?.headers || thisExtras?.headers || extras.headers) {
|
|
143
|
+
x.headers = { ...(shared.extras?.headers || {}), ...(thisExtras?.headers || {}), ...(extras.headers || {}) };
|
|
144
|
+
}
|
|
145
|
+
return x;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function query(out: Array<string>, classExtras: CallExtras|undefined, thisExtras: CallExtras|undefined, forbidden: Array<string>): void {
|
|
149
|
+
const ex: String2String = { ...(shared.extras?.query || {}), ...(classExtras?.query || {}), ...(thisExtras?.query || {}) };
|
|
150
|
+
for (const key in ex) {
|
|
151
|
+
if (forbidden.includes(key)) continue;
|
|
152
|
+
const value = ex[key];
|
|
153
|
+
out.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function headers(out: Headers, classExtras: CallExtras|undefined, thisExtras: CallExtras|undefined, forbidden: Array<string>): Headers {
|
|
158
|
+
const ex: String2String = { ...(shared.extras?.headers || {}), ...(classExtras?.headers || {}), ...(thisExtras?.headers || {}) };
|
|
159
|
+
for (const key in ex) {
|
|
160
|
+
if (forbidden.includes(key)) continue;
|
|
161
|
+
out.set(key, ex[key]);
|
|
162
|
+
}
|
|
163
|
+
return out;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export type LoaderFunction = (response: Response) => Promise<any>;
|
|
167
|
+
export type LoaderMap = Map<string, LoaderFunction>;
|
|
168
|
+
|
|
169
|
+
function blobLoader(response: Response): Promise<any> {
|
|
170
|
+
return response.blob();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function getLoader(contentType: string|null, loaders: LoaderMap|undefined, classArgsLoaders: LoaderMap|undefined, classLoaders: LoaderMap): LoaderFunction {
|
|
174
|
+
if (!contentType)
|
|
175
|
+
return blobLoader;
|
|
176
|
+
let ct = contentType.toLowerCase();
|
|
177
|
+
const idx = ct.indexOf(';')
|
|
178
|
+
if (-1 < idx)
|
|
179
|
+
ct = `${ct.slice(0, idx).trim()}; ${ct.slice(idx + 1).trim()}`;
|
|
180
|
+
let loader: LoaderFunction|undefined = undefined;
|
|
181
|
+
if (loaders !== undefined) {
|
|
182
|
+
loader = loaders.get(ct);
|
|
183
|
+
}
|
|
184
|
+
if (loader === undefined && classArgsLoaders !== undefined) {
|
|
185
|
+
loader = classArgsLoaders.get(ct)
|
|
186
|
+
}
|
|
187
|
+
if (loader === undefined) {
|
|
188
|
+
loader = classLoaders.get(ct);
|
|
189
|
+
}
|
|
190
|
+
return loader || blobLoader;
|
|
191
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<%= Gen.x.generator_info %>
|
|
2
|
+
|
|
3
|
+
export type {LoaderFunction, LoaderMap} from './helpers.d.ts';
|
|
4
|
+
export type {AllParameters, Shared} from './shared.d.ts';
|
|
5
|
+
export {shared} from './shared.js';
|
|
6
|
+
<%=
|
|
7
|
+
def export_lines(exported_names)
|
|
8
|
+
lines = []
|
|
9
|
+
imps = exported_names.import_types
|
|
10
|
+
lines.push("export type {#{imps.join(', ')}} from './#{exported_names.basename}.d.ts';") unless imps.empty?
|
|
11
|
+
imps = exported_names.imports
|
|
12
|
+
lines.push("export {#{imps.join(', ')}} from './#{exported_names.basename}.js';") unless imps.empty?
|
|
13
|
+
lines
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
out = []
|
|
17
|
+
out.concat(export_lines(Gen.x.schemas), export_lines(Gen.x.servers), export_lines(Gen.x.callclasses))
|
|
18
|
+
Gen.output.join(out)
|
|
19
|
+
%>
|