-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
112 lines (101 loc) · 2.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const { compileTemplate } = require('./src/template');
const { renderProgram, createEffect } = require('./src');
const BEFORE = `
// @ts-nocheck
// {{TITLE}} {{VERSION}}
// ---
// This file is automatically generated by openapi with preset effector-openapi-preset
// Do not edit this file directly. Instead open openapi config file and follow the link in "file"
import { createEffect } from '{{EFFECTOR}}';
import * as typed from 'typed-contracts';
import { {{REQUEST_NAME}} } from '{{REQUEST_PATH}}';
//#region prebuilt code
const custom = { any: (valueName: string, value: unknown): any => value }
export type GenericErrors =
| {
status: 'unexpected';
error: Error;
}
| {
status: 'unknown_status';
error: { status: number; body: unknown };
}
| {
status: 'validation_error';
error: typed.ValidationError;
};
type ErrorCodes = 400 | 401 | 402 | 403 | 404 | 405 | 406 | 500 | 501 | 502 | 503 | 505;
/**
* @throws
*/
function parseByStatus<
Variants extends string,
Contracts extends Record<number, [Variants, typed.Contract<any>]>,
Result extends {
[Code in keyof Contracts]: Contracts[Code] extends [infer Status, typed.Contract<infer T>]
? { status: Status; answer: T }
: never;
}
>(
name: string,
response: { status: number; body?: unknown },
contracts: Contracts,
): Result[Exclude<keyof Result, ErrorCodes>] {
const contractObject = contracts[response.status];
if (!contractObject) {
throw {
status: 'unknown_status',
error: {
status: response.status,
body: response.body,
},
};
}
const [status, contract] = contractObject;
const answer = contract(name, response.body);
if (answer instanceof typed.ValidationError) {
throw { status: 'validation_error', error: answer };
}
if (response.status >= 400) {
throw { status, error: answer };
}
return { status, answer } as Result[Exclude<keyof Result, ErrorCodes>];
}
//#endregion prebuilt code
`.trim();
const METHOD = `
/* --- */
//#region {{METHOD_NAME}}
{{METHOD_CONTENT}}
//#endregion {{METHOD_NAME}}
`.trim();
module.exports = ({
effectorImport = 'effector',
requestName = 'requestFx',
requestPath = './request',
} = {}) => ({
importRequest: 'disabled',
disableTypesGenerate: true,
templateFileNameCode: ({ swaggerData, changeCase }) =>
`${changeCase.paramCase(swaggerData.info.title)}.ts`,
templateCodeBefore: ({ swaggerData }) =>
compileTemplate(BEFORE, {
TITLE: swaggerData.info.title,
VERSION: swaggerData.info.version,
EFFECTOR: effectorImport,
REQUEST_NAME: requestName,
REQUEST_PATH: requestPath,
}),
templateRequestCode: (
{ name, method, url },
{ requestSwaggerData, changeCase },
) =>
compileTemplate(METHOD, {
METHOD_NAME: changeCase.camelCase(name),
METHOD_CONTENT: renderProgram(
createEffect({ name, path: url, method }, requestSwaggerData, {
requestName,
}),
),
}),
});