@arrirpc/codegen-utils 0.45.0
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +9 -0
- package/README.md +11 -0
- package/dist/index.cjs +197 -0
- package/dist/index.d.cts +72 -0
- package/dist/index.d.mts +72 -0
- package/dist/index.d.ts +72 -0
- package/dist/index.mjs +175 -0
- package/dist/testModels.cjs +241 -0
- package/dist/testModels.d.cts +168 -0
- package/dist/testModels.d.mts +168 -0
- package/dist/testModels.d.ts +168 -0
- package/dist/testModels.mjs +229 -0
- package/package.json +29 -0
package/LICENSE
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright 2024 Joshua Sosso
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
+
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# @arrirpc/codegen-utils
|
2
|
+
|
3
|
+
This library was generated with [Nx](https://nx.dev).
|
4
|
+
|
5
|
+
## Building
|
6
|
+
|
7
|
+
Run `nx build @arrirpc/codegen-utils` to build the library.
|
8
|
+
|
9
|
+
## Running unit tests
|
10
|
+
|
11
|
+
Run `nx test @arrirpc/codegen-utils` to execute the unit tests via [Vitest](https://vitest.dev).
|
package/dist/index.cjs
ADDED
@@ -0,0 +1,197 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
const scule = require('scule');
|
4
|
+
const jtdUtils = require('jtd-utils');
|
5
|
+
|
6
|
+
const HttpMethodValues = [
|
7
|
+
"get",
|
8
|
+
"post",
|
9
|
+
"put",
|
10
|
+
"patch",
|
11
|
+
"delete",
|
12
|
+
"head"
|
13
|
+
];
|
14
|
+
const isHttpMethod = (input) => {
|
15
|
+
if (typeof input !== "string") {
|
16
|
+
return false;
|
17
|
+
}
|
18
|
+
return HttpMethodValues.includes(input);
|
19
|
+
};
|
20
|
+
const isRpcHttpMethod = (input) => {
|
21
|
+
return isHttpMethod(input) && input !== "head";
|
22
|
+
};
|
23
|
+
const SCHEMA_VERSION = "0.0.5";
|
24
|
+
function isAppDefinition(input) {
|
25
|
+
if (typeof input !== "object") {
|
26
|
+
return false;
|
27
|
+
}
|
28
|
+
const inputObj = input;
|
29
|
+
if (typeof inputObj.arriSchemaVersion !== "string") {
|
30
|
+
return false;
|
31
|
+
}
|
32
|
+
if (typeof inputObj.procedures !== "object") {
|
33
|
+
return false;
|
34
|
+
}
|
35
|
+
if (typeof inputObj.definitions !== "object") {
|
36
|
+
return false;
|
37
|
+
}
|
38
|
+
return true;
|
39
|
+
}
|
40
|
+
function isRpcDefinitionBase(input) {
|
41
|
+
if (typeof input !== "object" || input === null) {
|
42
|
+
return false;
|
43
|
+
}
|
44
|
+
if ("params" in input && typeof input.params !== "undefined" && typeof input.params !== "string") {
|
45
|
+
return false;
|
46
|
+
}
|
47
|
+
if ("response" in input && typeof input.response !== "undefined" && typeof input.response !== "string") {
|
48
|
+
return false;
|
49
|
+
}
|
50
|
+
return "transport" in input && typeof input.transport === "string" && input.transport.length > 0 && "path" in input && typeof input.path === "string" && input.path.length > 0;
|
51
|
+
}
|
52
|
+
function isRpcDefinition(input) {
|
53
|
+
if (!isRpcDefinitionBase(input)) {
|
54
|
+
return false;
|
55
|
+
}
|
56
|
+
if (!("transport" in input) || typeof input.transport !== "string") {
|
57
|
+
return false;
|
58
|
+
}
|
59
|
+
if (input.transport === "http") {
|
60
|
+
return "method" in input && isRpcHttpMethod(input.method);
|
61
|
+
}
|
62
|
+
if (input.transport === "ws") {
|
63
|
+
return true;
|
64
|
+
}
|
65
|
+
if (input.transport.startsWith("custom:")) {
|
66
|
+
return true;
|
67
|
+
}
|
68
|
+
return false;
|
69
|
+
}
|
70
|
+
function isServiceDefinition(input) {
|
71
|
+
if (typeof input !== "object") {
|
72
|
+
return false;
|
73
|
+
}
|
74
|
+
for (const key of Object.keys(input)) {
|
75
|
+
if (typeof input[key] !== "object") {
|
76
|
+
return false;
|
77
|
+
}
|
78
|
+
}
|
79
|
+
return true;
|
80
|
+
}
|
81
|
+
function unflattenProcedures(procedures) {
|
82
|
+
return unflattenObject(procedures);
|
83
|
+
}
|
84
|
+
function unflattenObject(data) {
|
85
|
+
if (Object(data) !== data || Array.isArray(data))
|
86
|
+
return data;
|
87
|
+
const regex = /\.?([^.[\]]+)|\[(\d+)\]/g;
|
88
|
+
const result = {};
|
89
|
+
for (const p in data) {
|
90
|
+
let cur = result;
|
91
|
+
let prop = "";
|
92
|
+
let m;
|
93
|
+
while (m = regex.exec(p)) {
|
94
|
+
cur = cur[prop] || (cur[prop] = m[2] ? [] : {});
|
95
|
+
prop = m[2] || m[1];
|
96
|
+
}
|
97
|
+
cur[prop] = data[p];
|
98
|
+
}
|
99
|
+
return result[""] || result;
|
100
|
+
}
|
101
|
+
const removeDisallowedChars = (input, disallowedChars) => {
|
102
|
+
let result = input;
|
103
|
+
for (const char of disallowedChars) {
|
104
|
+
if (result.includes(char)) {
|
105
|
+
result = result.split(char).join("");
|
106
|
+
}
|
107
|
+
}
|
108
|
+
return result;
|
109
|
+
};
|
110
|
+
const stringStartsWithNumber = (input) => input.length !== 0 && !Number.isNaN(Number(input.charAt(0)));
|
111
|
+
function setNestedObjectProperty(targetProp, value, object) {
|
112
|
+
const parts = targetProp.split(".");
|
113
|
+
let current = object;
|
114
|
+
for (let i = 0; i < parts.length; i++) {
|
115
|
+
const key = parts[i];
|
116
|
+
if (i === parts.length - 1) {
|
117
|
+
current[key] = value;
|
118
|
+
} else {
|
119
|
+
if (!current[key]) {
|
120
|
+
current[key] = {};
|
121
|
+
}
|
122
|
+
current = current[key];
|
123
|
+
}
|
124
|
+
}
|
125
|
+
return object;
|
126
|
+
}
|
127
|
+
function normalizeWhitespace(input) {
|
128
|
+
if (input.includes("\n\n")) {
|
129
|
+
return normalizeWhitespace(input.split("\n\n").join("\n"));
|
130
|
+
}
|
131
|
+
const lines = [];
|
132
|
+
for (const line of input.split("\n")) {
|
133
|
+
lines.push(line.trim());
|
134
|
+
}
|
135
|
+
const result = lines.join("\n").trim();
|
136
|
+
if (result.includes("\n\n")) {
|
137
|
+
return normalizeWhitespace(result.split("\n\n").join("\n"));
|
138
|
+
}
|
139
|
+
return result;
|
140
|
+
}
|
141
|
+
function defineClientGeneratorPlugin(plugin) {
|
142
|
+
return plugin;
|
143
|
+
}
|
144
|
+
function createAppDefinition(input) {
|
145
|
+
const definitions = { ...input.definitions };
|
146
|
+
const procedures = {};
|
147
|
+
for (const key of Object.keys(input.procedures)) {
|
148
|
+
const def = input.procedures[key];
|
149
|
+
let paramName;
|
150
|
+
if (def.params) {
|
151
|
+
paramName = def.params.metadata?.id ?? scule.pascalCase(`${key.split(".").join("_")}Params`);
|
152
|
+
definitions[paramName] = def.params;
|
153
|
+
}
|
154
|
+
let responseName;
|
155
|
+
if (def.response) {
|
156
|
+
responseName = def.response.metadata?.id ?? scule.pascalCase(`${key.split(".").join("_")}Response`);
|
157
|
+
definitions[responseName] = def.response;
|
158
|
+
}
|
159
|
+
delete def.params;
|
160
|
+
delete def.response;
|
161
|
+
procedures[key] = {
|
162
|
+
...def,
|
163
|
+
params: paramName,
|
164
|
+
response: responseName
|
165
|
+
};
|
166
|
+
}
|
167
|
+
const result = {
|
168
|
+
arriSchemaVersion: "0.0.5",
|
169
|
+
...input,
|
170
|
+
procedures,
|
171
|
+
definitions
|
172
|
+
};
|
173
|
+
return result;
|
174
|
+
}
|
175
|
+
|
176
|
+
exports.HttpMethodValues = HttpMethodValues;
|
177
|
+
exports.SCHEMA_VERSION = SCHEMA_VERSION;
|
178
|
+
exports.createAppDefinition = createAppDefinition;
|
179
|
+
exports.defineClientGeneratorPlugin = defineClientGeneratorPlugin;
|
180
|
+
exports.isAppDefinition = isAppDefinition;
|
181
|
+
exports.isHttpMethod = isHttpMethod;
|
182
|
+
exports.isRpcDefinition = isRpcDefinition;
|
183
|
+
exports.isRpcDefinitionBase = isRpcDefinitionBase;
|
184
|
+
exports.isRpcHttpMethod = isRpcHttpMethod;
|
185
|
+
exports.isServiceDefinition = isServiceDefinition;
|
186
|
+
exports.normalizeWhitespace = normalizeWhitespace;
|
187
|
+
exports.removeDisallowedChars = removeDisallowedChars;
|
188
|
+
exports.setNestedObjectProperty = setNestedObjectProperty;
|
189
|
+
exports.stringStartsWithNumber = stringStartsWithNumber;
|
190
|
+
exports.unflattenObject = unflattenObject;
|
191
|
+
exports.unflattenProcedures = unflattenProcedures;
|
192
|
+
Object.keys(scule).forEach(function (k) {
|
193
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = scule[k];
|
194
|
+
});
|
195
|
+
Object.keys(jtdUtils).forEach(function (k) {
|
196
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = jtdUtils[k];
|
197
|
+
});
|
package/dist/index.d.cts
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
import { SchemaFormProperties, SchemaFormDiscriminator } from 'jtd-utils';
|
2
|
+
export * from 'jtd-utils';
|
3
|
+
export * from 'scule';
|
4
|
+
|
5
|
+
declare const HttpMethodValues: readonly ["get", "post", "put", "patch", "delete", "head"];
|
6
|
+
type HttpMethod = (typeof HttpMethodValues)[number];
|
7
|
+
type RpcHttpMethod = Exclude<HttpMethod, "head">;
|
8
|
+
declare const isHttpMethod: (input: any) => input is "get" | "post" | "put" | "patch" | "delete" | "head";
|
9
|
+
declare const isRpcHttpMethod: (input: any) => input is RpcHttpMethod;
|
10
|
+
declare const SCHEMA_VERSION = "0.0.5";
|
11
|
+
interface AppDefinition {
|
12
|
+
arriSchemaVersion: typeof SCHEMA_VERSION;
|
13
|
+
info?: {
|
14
|
+
title?: string;
|
15
|
+
description?: string;
|
16
|
+
version?: string;
|
17
|
+
[key: string]: string | undefined;
|
18
|
+
};
|
19
|
+
externalDocs?: {
|
20
|
+
description?: string;
|
21
|
+
url: string;
|
22
|
+
};
|
23
|
+
procedures: Record<string, RpcDefinition<string>>;
|
24
|
+
definitions: Record<string, SchemaFormProperties | SchemaFormDiscriminator>;
|
25
|
+
}
|
26
|
+
declare function isAppDefinition(input: unknown): input is AppDefinition;
|
27
|
+
interface RpcDefinitionBase<T = string> {
|
28
|
+
path: string;
|
29
|
+
params?: T;
|
30
|
+
response?: T;
|
31
|
+
description?: string;
|
32
|
+
isDeprecated?: boolean;
|
33
|
+
}
|
34
|
+
interface HttpRpcDefinition<T = string> extends RpcDefinitionBase<T> {
|
35
|
+
transport: "http";
|
36
|
+
method: RpcHttpMethod;
|
37
|
+
isEventStream?: boolean;
|
38
|
+
}
|
39
|
+
interface WsRpcDefinition<T = string> extends RpcDefinitionBase<T> {
|
40
|
+
transport: "ws";
|
41
|
+
}
|
42
|
+
interface CustomRpcDefinition<T = string> extends RpcDefinitionBase<T> {
|
43
|
+
transport: `custom:${string}`;
|
44
|
+
[key: string]: unknown;
|
45
|
+
}
|
46
|
+
type RpcDefinition<T = string> = HttpRpcDefinition<T> | WsRpcDefinition<T> | CustomRpcDefinition<T>;
|
47
|
+
declare function isRpcDefinitionBase(input: unknown): input is RpcDefinitionBase;
|
48
|
+
declare function isRpcDefinition(input: unknown): input is RpcDefinition;
|
49
|
+
interface ServiceDefinition {
|
50
|
+
[key: string]: RpcDefinition | ServiceDefinition;
|
51
|
+
}
|
52
|
+
declare function isServiceDefinition(input: any): input is ServiceDefinition;
|
53
|
+
declare function unflattenProcedures(procedures: AppDefinition["procedures"]): Record<string, RpcDefinition | ServiceDefinition>;
|
54
|
+
declare function unflattenObject(data: Record<string, any>): any;
|
55
|
+
declare const removeDisallowedChars: (input: string, disallowedChars: string) => string;
|
56
|
+
declare const stringStartsWithNumber: (input: string) => boolean;
|
57
|
+
declare function setNestedObjectProperty<T>(targetProp: string, value: T, object: Record<any, any>): Record<any, any>;
|
58
|
+
declare function normalizeWhitespace(input: string): string;
|
59
|
+
interface ClientGenerator<TOptions extends Record<string, any> | undefined> {
|
60
|
+
generator: (def: AppDefinition, isDevServer?: boolean) => any;
|
61
|
+
options: TOptions;
|
62
|
+
}
|
63
|
+
type ClientGeneratorPlugin<TOptions extends Record<string, any> | undefined> = (options: TOptions) => ClientGenerator<TOptions>;
|
64
|
+
declare function defineClientGeneratorPlugin<TOptions extends Record<string, any> | undefined>(plugin: ClientGeneratorPlugin<TOptions>): ClientGeneratorPlugin<TOptions>;
|
65
|
+
type RpcDefinitionHelper = RpcDefinition<SchemaFormProperties | SchemaFormDiscriminator>;
|
66
|
+
type AppDefinitionHelper = Omit<AppDefinition, "procedures" | "definitions" | "arriSchemaVersion"> & {
|
67
|
+
procedures: Record<string, RpcDefinitionHelper>;
|
68
|
+
definitions?: AppDefinition["definitions"];
|
69
|
+
};
|
70
|
+
declare function createAppDefinition(input: AppDefinitionHelper): AppDefinition;
|
71
|
+
|
72
|
+
export { type AppDefinition, type ClientGenerator, type ClientGeneratorPlugin, type CustomRpcDefinition, type HttpMethod, HttpMethodValues, type HttpRpcDefinition, type RpcDefinition, type RpcDefinitionBase, type RpcHttpMethod, SCHEMA_VERSION, type ServiceDefinition, type WsRpcDefinition, createAppDefinition, defineClientGeneratorPlugin, isAppDefinition, isHttpMethod, isRpcDefinition, isRpcDefinitionBase, isRpcHttpMethod, isServiceDefinition, normalizeWhitespace, removeDisallowedChars, setNestedObjectProperty, stringStartsWithNumber, unflattenObject, unflattenProcedures };
|
package/dist/index.d.mts
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
import { SchemaFormProperties, SchemaFormDiscriminator } from 'jtd-utils';
|
2
|
+
export * from 'jtd-utils';
|
3
|
+
export * from 'scule';
|
4
|
+
|
5
|
+
declare const HttpMethodValues: readonly ["get", "post", "put", "patch", "delete", "head"];
|
6
|
+
type HttpMethod = (typeof HttpMethodValues)[number];
|
7
|
+
type RpcHttpMethod = Exclude<HttpMethod, "head">;
|
8
|
+
declare const isHttpMethod: (input: any) => input is "get" | "post" | "put" | "patch" | "delete" | "head";
|
9
|
+
declare const isRpcHttpMethod: (input: any) => input is RpcHttpMethod;
|
10
|
+
declare const SCHEMA_VERSION = "0.0.5";
|
11
|
+
interface AppDefinition {
|
12
|
+
arriSchemaVersion: typeof SCHEMA_VERSION;
|
13
|
+
info?: {
|
14
|
+
title?: string;
|
15
|
+
description?: string;
|
16
|
+
version?: string;
|
17
|
+
[key: string]: string | undefined;
|
18
|
+
};
|
19
|
+
externalDocs?: {
|
20
|
+
description?: string;
|
21
|
+
url: string;
|
22
|
+
};
|
23
|
+
procedures: Record<string, RpcDefinition<string>>;
|
24
|
+
definitions: Record<string, SchemaFormProperties | SchemaFormDiscriminator>;
|
25
|
+
}
|
26
|
+
declare function isAppDefinition(input: unknown): input is AppDefinition;
|
27
|
+
interface RpcDefinitionBase<T = string> {
|
28
|
+
path: string;
|
29
|
+
params?: T;
|
30
|
+
response?: T;
|
31
|
+
description?: string;
|
32
|
+
isDeprecated?: boolean;
|
33
|
+
}
|
34
|
+
interface HttpRpcDefinition<T = string> extends RpcDefinitionBase<T> {
|
35
|
+
transport: "http";
|
36
|
+
method: RpcHttpMethod;
|
37
|
+
isEventStream?: boolean;
|
38
|
+
}
|
39
|
+
interface WsRpcDefinition<T = string> extends RpcDefinitionBase<T> {
|
40
|
+
transport: "ws";
|
41
|
+
}
|
42
|
+
interface CustomRpcDefinition<T = string> extends RpcDefinitionBase<T> {
|
43
|
+
transport: `custom:${string}`;
|
44
|
+
[key: string]: unknown;
|
45
|
+
}
|
46
|
+
type RpcDefinition<T = string> = HttpRpcDefinition<T> | WsRpcDefinition<T> | CustomRpcDefinition<T>;
|
47
|
+
declare function isRpcDefinitionBase(input: unknown): input is RpcDefinitionBase;
|
48
|
+
declare function isRpcDefinition(input: unknown): input is RpcDefinition;
|
49
|
+
interface ServiceDefinition {
|
50
|
+
[key: string]: RpcDefinition | ServiceDefinition;
|
51
|
+
}
|
52
|
+
declare function isServiceDefinition(input: any): input is ServiceDefinition;
|
53
|
+
declare function unflattenProcedures(procedures: AppDefinition["procedures"]): Record<string, RpcDefinition | ServiceDefinition>;
|
54
|
+
declare function unflattenObject(data: Record<string, any>): any;
|
55
|
+
declare const removeDisallowedChars: (input: string, disallowedChars: string) => string;
|
56
|
+
declare const stringStartsWithNumber: (input: string) => boolean;
|
57
|
+
declare function setNestedObjectProperty<T>(targetProp: string, value: T, object: Record<any, any>): Record<any, any>;
|
58
|
+
declare function normalizeWhitespace(input: string): string;
|
59
|
+
interface ClientGenerator<TOptions extends Record<string, any> | undefined> {
|
60
|
+
generator: (def: AppDefinition, isDevServer?: boolean) => any;
|
61
|
+
options: TOptions;
|
62
|
+
}
|
63
|
+
type ClientGeneratorPlugin<TOptions extends Record<string, any> | undefined> = (options: TOptions) => ClientGenerator<TOptions>;
|
64
|
+
declare function defineClientGeneratorPlugin<TOptions extends Record<string, any> | undefined>(plugin: ClientGeneratorPlugin<TOptions>): ClientGeneratorPlugin<TOptions>;
|
65
|
+
type RpcDefinitionHelper = RpcDefinition<SchemaFormProperties | SchemaFormDiscriminator>;
|
66
|
+
type AppDefinitionHelper = Omit<AppDefinition, "procedures" | "definitions" | "arriSchemaVersion"> & {
|
67
|
+
procedures: Record<string, RpcDefinitionHelper>;
|
68
|
+
definitions?: AppDefinition["definitions"];
|
69
|
+
};
|
70
|
+
declare function createAppDefinition(input: AppDefinitionHelper): AppDefinition;
|
71
|
+
|
72
|
+
export { type AppDefinition, type ClientGenerator, type ClientGeneratorPlugin, type CustomRpcDefinition, type HttpMethod, HttpMethodValues, type HttpRpcDefinition, type RpcDefinition, type RpcDefinitionBase, type RpcHttpMethod, SCHEMA_VERSION, type ServiceDefinition, type WsRpcDefinition, createAppDefinition, defineClientGeneratorPlugin, isAppDefinition, isHttpMethod, isRpcDefinition, isRpcDefinitionBase, isRpcHttpMethod, isServiceDefinition, normalizeWhitespace, removeDisallowedChars, setNestedObjectProperty, stringStartsWithNumber, unflattenObject, unflattenProcedures };
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
import { SchemaFormProperties, SchemaFormDiscriminator } from 'jtd-utils';
|
2
|
+
export * from 'jtd-utils';
|
3
|
+
export * from 'scule';
|
4
|
+
|
5
|
+
declare const HttpMethodValues: readonly ["get", "post", "put", "patch", "delete", "head"];
|
6
|
+
type HttpMethod = (typeof HttpMethodValues)[number];
|
7
|
+
type RpcHttpMethod = Exclude<HttpMethod, "head">;
|
8
|
+
declare const isHttpMethod: (input: any) => input is "get" | "post" | "put" | "patch" | "delete" | "head";
|
9
|
+
declare const isRpcHttpMethod: (input: any) => input is RpcHttpMethod;
|
10
|
+
declare const SCHEMA_VERSION = "0.0.5";
|
11
|
+
interface AppDefinition {
|
12
|
+
arriSchemaVersion: typeof SCHEMA_VERSION;
|
13
|
+
info?: {
|
14
|
+
title?: string;
|
15
|
+
description?: string;
|
16
|
+
version?: string;
|
17
|
+
[key: string]: string | undefined;
|
18
|
+
};
|
19
|
+
externalDocs?: {
|
20
|
+
description?: string;
|
21
|
+
url: string;
|
22
|
+
};
|
23
|
+
procedures: Record<string, RpcDefinition<string>>;
|
24
|
+
definitions: Record<string, SchemaFormProperties | SchemaFormDiscriminator>;
|
25
|
+
}
|
26
|
+
declare function isAppDefinition(input: unknown): input is AppDefinition;
|
27
|
+
interface RpcDefinitionBase<T = string> {
|
28
|
+
path: string;
|
29
|
+
params?: T;
|
30
|
+
response?: T;
|
31
|
+
description?: string;
|
32
|
+
isDeprecated?: boolean;
|
33
|
+
}
|
34
|
+
interface HttpRpcDefinition<T = string> extends RpcDefinitionBase<T> {
|
35
|
+
transport: "http";
|
36
|
+
method: RpcHttpMethod;
|
37
|
+
isEventStream?: boolean;
|
38
|
+
}
|
39
|
+
interface WsRpcDefinition<T = string> extends RpcDefinitionBase<T> {
|
40
|
+
transport: "ws";
|
41
|
+
}
|
42
|
+
interface CustomRpcDefinition<T = string> extends RpcDefinitionBase<T> {
|
43
|
+
transport: `custom:${string}`;
|
44
|
+
[key: string]: unknown;
|
45
|
+
}
|
46
|
+
type RpcDefinition<T = string> = HttpRpcDefinition<T> | WsRpcDefinition<T> | CustomRpcDefinition<T>;
|
47
|
+
declare function isRpcDefinitionBase(input: unknown): input is RpcDefinitionBase;
|
48
|
+
declare function isRpcDefinition(input: unknown): input is RpcDefinition;
|
49
|
+
interface ServiceDefinition {
|
50
|
+
[key: string]: RpcDefinition | ServiceDefinition;
|
51
|
+
}
|
52
|
+
declare function isServiceDefinition(input: any): input is ServiceDefinition;
|
53
|
+
declare function unflattenProcedures(procedures: AppDefinition["procedures"]): Record<string, RpcDefinition | ServiceDefinition>;
|
54
|
+
declare function unflattenObject(data: Record<string, any>): any;
|
55
|
+
declare const removeDisallowedChars: (input: string, disallowedChars: string) => string;
|
56
|
+
declare const stringStartsWithNumber: (input: string) => boolean;
|
57
|
+
declare function setNestedObjectProperty<T>(targetProp: string, value: T, object: Record<any, any>): Record<any, any>;
|
58
|
+
declare function normalizeWhitespace(input: string): string;
|
59
|
+
interface ClientGenerator<TOptions extends Record<string, any> | undefined> {
|
60
|
+
generator: (def: AppDefinition, isDevServer?: boolean) => any;
|
61
|
+
options: TOptions;
|
62
|
+
}
|
63
|
+
type ClientGeneratorPlugin<TOptions extends Record<string, any> | undefined> = (options: TOptions) => ClientGenerator<TOptions>;
|
64
|
+
declare function defineClientGeneratorPlugin<TOptions extends Record<string, any> | undefined>(plugin: ClientGeneratorPlugin<TOptions>): ClientGeneratorPlugin<TOptions>;
|
65
|
+
type RpcDefinitionHelper = RpcDefinition<SchemaFormProperties | SchemaFormDiscriminator>;
|
66
|
+
type AppDefinitionHelper = Omit<AppDefinition, "procedures" | "definitions" | "arriSchemaVersion"> & {
|
67
|
+
procedures: Record<string, RpcDefinitionHelper>;
|
68
|
+
definitions?: AppDefinition["definitions"];
|
69
|
+
};
|
70
|
+
declare function createAppDefinition(input: AppDefinitionHelper): AppDefinition;
|
71
|
+
|
72
|
+
export { type AppDefinition, type ClientGenerator, type ClientGeneratorPlugin, type CustomRpcDefinition, type HttpMethod, HttpMethodValues, type HttpRpcDefinition, type RpcDefinition, type RpcDefinitionBase, type RpcHttpMethod, SCHEMA_VERSION, type ServiceDefinition, type WsRpcDefinition, createAppDefinition, defineClientGeneratorPlugin, isAppDefinition, isHttpMethod, isRpcDefinition, isRpcDefinitionBase, isRpcHttpMethod, isServiceDefinition, normalizeWhitespace, removeDisallowedChars, setNestedObjectProperty, stringStartsWithNumber, unflattenObject, unflattenProcedures };
|
package/dist/index.mjs
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
import { pascalCase } from 'scule';
|
2
|
+
export * from 'scule';
|
3
|
+
export * from 'jtd-utils';
|
4
|
+
|
5
|
+
const HttpMethodValues = [
|
6
|
+
"get",
|
7
|
+
"post",
|
8
|
+
"put",
|
9
|
+
"patch",
|
10
|
+
"delete",
|
11
|
+
"head"
|
12
|
+
];
|
13
|
+
const isHttpMethod = (input) => {
|
14
|
+
if (typeof input !== "string") {
|
15
|
+
return false;
|
16
|
+
}
|
17
|
+
return HttpMethodValues.includes(input);
|
18
|
+
};
|
19
|
+
const isRpcHttpMethod = (input) => {
|
20
|
+
return isHttpMethod(input) && input !== "head";
|
21
|
+
};
|
22
|
+
const SCHEMA_VERSION = "0.0.5";
|
23
|
+
function isAppDefinition(input) {
|
24
|
+
if (typeof input !== "object") {
|
25
|
+
return false;
|
26
|
+
}
|
27
|
+
const inputObj = input;
|
28
|
+
if (typeof inputObj.arriSchemaVersion !== "string") {
|
29
|
+
return false;
|
30
|
+
}
|
31
|
+
if (typeof inputObj.procedures !== "object") {
|
32
|
+
return false;
|
33
|
+
}
|
34
|
+
if (typeof inputObj.definitions !== "object") {
|
35
|
+
return false;
|
36
|
+
}
|
37
|
+
return true;
|
38
|
+
}
|
39
|
+
function isRpcDefinitionBase(input) {
|
40
|
+
if (typeof input !== "object" || input === null) {
|
41
|
+
return false;
|
42
|
+
}
|
43
|
+
if ("params" in input && typeof input.params !== "undefined" && typeof input.params !== "string") {
|
44
|
+
return false;
|
45
|
+
}
|
46
|
+
if ("response" in input && typeof input.response !== "undefined" && typeof input.response !== "string") {
|
47
|
+
return false;
|
48
|
+
}
|
49
|
+
return "transport" in input && typeof input.transport === "string" && input.transport.length > 0 && "path" in input && typeof input.path === "string" && input.path.length > 0;
|
50
|
+
}
|
51
|
+
function isRpcDefinition(input) {
|
52
|
+
if (!isRpcDefinitionBase(input)) {
|
53
|
+
return false;
|
54
|
+
}
|
55
|
+
if (!("transport" in input) || typeof input.transport !== "string") {
|
56
|
+
return false;
|
57
|
+
}
|
58
|
+
if (input.transport === "http") {
|
59
|
+
return "method" in input && isRpcHttpMethod(input.method);
|
60
|
+
}
|
61
|
+
if (input.transport === "ws") {
|
62
|
+
return true;
|
63
|
+
}
|
64
|
+
if (input.transport.startsWith("custom:")) {
|
65
|
+
return true;
|
66
|
+
}
|
67
|
+
return false;
|
68
|
+
}
|
69
|
+
function isServiceDefinition(input) {
|
70
|
+
if (typeof input !== "object") {
|
71
|
+
return false;
|
72
|
+
}
|
73
|
+
for (const key of Object.keys(input)) {
|
74
|
+
if (typeof input[key] !== "object") {
|
75
|
+
return false;
|
76
|
+
}
|
77
|
+
}
|
78
|
+
return true;
|
79
|
+
}
|
80
|
+
function unflattenProcedures(procedures) {
|
81
|
+
return unflattenObject(procedures);
|
82
|
+
}
|
83
|
+
function unflattenObject(data) {
|
84
|
+
if (Object(data) !== data || Array.isArray(data))
|
85
|
+
return data;
|
86
|
+
const regex = /\.?([^.[\]]+)|\[(\d+)\]/g;
|
87
|
+
const result = {};
|
88
|
+
for (const p in data) {
|
89
|
+
let cur = result;
|
90
|
+
let prop = "";
|
91
|
+
let m;
|
92
|
+
while (m = regex.exec(p)) {
|
93
|
+
cur = cur[prop] || (cur[prop] = m[2] ? [] : {});
|
94
|
+
prop = m[2] || m[1];
|
95
|
+
}
|
96
|
+
cur[prop] = data[p];
|
97
|
+
}
|
98
|
+
return result[""] || result;
|
99
|
+
}
|
100
|
+
const removeDisallowedChars = (input, disallowedChars) => {
|
101
|
+
let result = input;
|
102
|
+
for (const char of disallowedChars) {
|
103
|
+
if (result.includes(char)) {
|
104
|
+
result = result.split(char).join("");
|
105
|
+
}
|
106
|
+
}
|
107
|
+
return result;
|
108
|
+
};
|
109
|
+
const stringStartsWithNumber = (input) => input.length !== 0 && !Number.isNaN(Number(input.charAt(0)));
|
110
|
+
function setNestedObjectProperty(targetProp, value, object) {
|
111
|
+
const parts = targetProp.split(".");
|
112
|
+
let current = object;
|
113
|
+
for (let i = 0; i < parts.length; i++) {
|
114
|
+
const key = parts[i];
|
115
|
+
if (i === parts.length - 1) {
|
116
|
+
current[key] = value;
|
117
|
+
} else {
|
118
|
+
if (!current[key]) {
|
119
|
+
current[key] = {};
|
120
|
+
}
|
121
|
+
current = current[key];
|
122
|
+
}
|
123
|
+
}
|
124
|
+
return object;
|
125
|
+
}
|
126
|
+
function normalizeWhitespace(input) {
|
127
|
+
if (input.includes("\n\n")) {
|
128
|
+
return normalizeWhitespace(input.split("\n\n").join("\n"));
|
129
|
+
}
|
130
|
+
const lines = [];
|
131
|
+
for (const line of input.split("\n")) {
|
132
|
+
lines.push(line.trim());
|
133
|
+
}
|
134
|
+
const result = lines.join("\n").trim();
|
135
|
+
if (result.includes("\n\n")) {
|
136
|
+
return normalizeWhitespace(result.split("\n\n").join("\n"));
|
137
|
+
}
|
138
|
+
return result;
|
139
|
+
}
|
140
|
+
function defineClientGeneratorPlugin(plugin) {
|
141
|
+
return plugin;
|
142
|
+
}
|
143
|
+
function createAppDefinition(input) {
|
144
|
+
const definitions = { ...input.definitions };
|
145
|
+
const procedures = {};
|
146
|
+
for (const key of Object.keys(input.procedures)) {
|
147
|
+
const def = input.procedures[key];
|
148
|
+
let paramName;
|
149
|
+
if (def.params) {
|
150
|
+
paramName = def.params.metadata?.id ?? pascalCase(`${key.split(".").join("_")}Params`);
|
151
|
+
definitions[paramName] = def.params;
|
152
|
+
}
|
153
|
+
let responseName;
|
154
|
+
if (def.response) {
|
155
|
+
responseName = def.response.metadata?.id ?? pascalCase(`${key.split(".").join("_")}Response`);
|
156
|
+
definitions[responseName] = def.response;
|
157
|
+
}
|
158
|
+
delete def.params;
|
159
|
+
delete def.response;
|
160
|
+
procedures[key] = {
|
161
|
+
...def,
|
162
|
+
params: paramName,
|
163
|
+
response: responseName
|
164
|
+
};
|
165
|
+
}
|
166
|
+
const result = {
|
167
|
+
arriSchemaVersion: "0.0.5",
|
168
|
+
...input,
|
169
|
+
procedures,
|
170
|
+
definitions
|
171
|
+
};
|
172
|
+
return result;
|
173
|
+
}
|
174
|
+
|
175
|
+
export { HttpMethodValues, SCHEMA_VERSION, createAppDefinition, defineClientGeneratorPlugin, isAppDefinition, isHttpMethod, isRpcDefinition, isRpcDefinitionBase, isRpcHttpMethod, isServiceDefinition, normalizeWhitespace, removeDisallowedChars, setNestedObjectProperty, stringStartsWithNumber, unflattenObject, unflattenProcedures };
|