-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.ts
More file actions
138 lines (127 loc) · 3.43 KB
/
lib.ts
File metadata and controls
138 lines (127 loc) · 3.43 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
export const DEBUG = (m: any, p = { depth: null }) => {
const location = new Error()?.stack?.split('\n')[2].match(/.*at (?<call>[^ ]+).*\/(?<file>[^:]+):(?<line>\d+).*/);
const here = `${location?.groups?.call} ${location?.groups?.file}:${location?.groups?.line}`;
console.dir(m, p);
console.warn(`logged at ${here}\n`);
};
export const camelCase = (str: string, firstCapital: boolean = false) => {
if (firstCapital) str = ' ' + str;
return str.replace(/^([A-Z])|[\s-_](\w)/g, (_, p1, p2) => (p2 ? p2.toUpperCase() : p1.toLowerCase()));
};
export const upper = (name: string) => `${name[0].toUpperCase()}${name.slice(1)}`;
export const dashCase = (str: string) => str.replace(/(?:([a-z])([A-Z]))|(?:((?!^)[A-Z])([a-z]))/g, '$1-$3$2$4').toLowerCase();
export const oasTypeToTypescript = (refName: string) => refName.replace(/^([$@#\w-]+\/)+/, '')?.replace(/[^\w_$]/g, '_') || '';
export const oasKeyToObjectKey = (name: string): string =>
/^[a-zA-Z_\$#][0-9a-zA-Z_\$]*$/.test(name || '') ? name : `'${name}'`;
export const TypeGenTypes = {
ref: 'REF',
path: 'PATH',
method: 'METHOD',
schema: 'SCHEMA',
model: 'MODEL',
parameter: 'PARAMETER',
queryParameter: 'QUERY_PARAMETER',
headersParameter: 'HEADER_PARAMETER',
pathParameter: 'PATH_PARAMETER',
securityScheme: 'SECURITY_SCHEME',
response: 'RESPONSE',
requestBody: 'REQUEST_BODY',
primitive: 'PRIMITIVE',
remoteRef: 'REMOTE_REF',
methodParam: 'METHOD_PARAM',
} as const;
export const ModelTypes = {
object: 'object',
number: 'number',
integer: 'integer',
boolean: 'boolean',
null: 'null',
string: 'string',
array: 'array',
http: 'http',
apiKey: 'apiKey',
oauth2: 'oauth2',
openIdConnect: 'openIdConnect',
basic: 'basic',
allOf: 'allOf',
oneOf: 'oneOf',
anyOf: 'anyOf',
} as const;
export const schemaTypeHints = {
schemas: 'SchemaObject',
responses: 'ResponseObject',
parameters: 'ParameterObject',
examples: 'ExampleObject',
requestBodies: 'RequestBodyObject',
headers: 'HeaderObject',
securitySchemes: 'SecuritySchemeObject',
links: 'LinkObject',
callbacks: 'CallbackObject',
} as const;
export const hintToTypegenType = {
SchemaObject: TypeGenTypes.schema,
ResponseObject: TypeGenTypes.response,
ParameterObject: TypeGenTypes.parameter,
RequestBodyObject: TypeGenTypes.requestBody,
HeaderObject: TypeGenTypes.headersParameter,
SecuritySchemeObject: TypeGenTypes.securityScheme,
} as const;
export const methods = {
get: true,
put: true,
post: true,
delete: true,
options: true,
head: true,
patch: true,
trace: true,
} as const;
export const validators = {
multipleOf: true,
maximum: true,
exclusiveMaximum: true,
minimum: true,
exclusiveMinimum: true,
maxLength: true,
minLength: true,
pattern: true,
maxItems: true,
minItems: true,
uniqueItems: true,
maxContains: true,
minContains: true,
maxProperties: true,
minProperties: true,
required: true,
dependentRequired: true,
nullable: true,
};
export const omitFromExtras = {
type: true,
format: true,
schema: true,
items: true,
$ref: true,
properties: true,
// default: true,
// required: true,
enum: true,
description: true,
additionalProperties: true,
allOf: true,
anyOf: true,
oneOf: true,
};
export const securityTypes = {
http: true,
apiKey: true,
oauth2: true,
openIdConnect: true,
};
export const primitives = {
integer: true,
number: true,
string: true,
boolean: true,
null: true,
};