@arrirpc/type-defs 0.70.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 +3 -0
- package/dist/index.cjs +247 -0
- package/dist/index.d.cts +109 -0
- package/dist/index.d.mts +109 -0
- package/dist/index.d.ts +109 -0
- package/dist/index.mjs +225 -0
- package/package.json +27 -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
package/dist/index.cjs
ADDED
@@ -0,0 +1,247 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
const scule = require('scule');
|
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.7";
|
23
|
+
function isAppDefinition(input) {
|
24
|
+
if (typeof input !== "object") {
|
25
|
+
return false;
|
26
|
+
}
|
27
|
+
const inputObj = input;
|
28
|
+
if (typeof inputObj.schemaVersion !== "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 defineGeneratorPlugin(plugin) {
|
81
|
+
return plugin;
|
82
|
+
}
|
83
|
+
function createAppDefinition(input) {
|
84
|
+
const definitions = { ...input.definitions };
|
85
|
+
const procedures = {};
|
86
|
+
for (const key of Object.keys(input.procedures)) {
|
87
|
+
const def = input.procedures[key];
|
88
|
+
let paramName;
|
89
|
+
if (def.params) {
|
90
|
+
paramName = def.params.metadata?.id ?? scule.pascalCase(`${key.split(".").join("_")}Params`);
|
91
|
+
definitions[paramName] = def.params;
|
92
|
+
}
|
93
|
+
let responseName;
|
94
|
+
if (def.response) {
|
95
|
+
responseName = def.response.metadata?.id ?? scule.pascalCase(`${key.split(".").join("_")}Response`);
|
96
|
+
definitions[responseName] = def.response;
|
97
|
+
}
|
98
|
+
delete def.params;
|
99
|
+
delete def.response;
|
100
|
+
procedures[key] = {
|
101
|
+
...def,
|
102
|
+
params: paramName,
|
103
|
+
response: responseName
|
104
|
+
};
|
105
|
+
}
|
106
|
+
const result = {
|
107
|
+
schemaVersion: "0.0.7",
|
108
|
+
...input,
|
109
|
+
procedures,
|
110
|
+
definitions
|
111
|
+
};
|
112
|
+
return result;
|
113
|
+
}
|
114
|
+
|
115
|
+
function isObject(input) {
|
116
|
+
return typeof input === "object" && input !== null;
|
117
|
+
}
|
118
|
+
function isSchema(input) {
|
119
|
+
const allowedProperties = [
|
120
|
+
"metadata",
|
121
|
+
"nullable",
|
122
|
+
"type",
|
123
|
+
"enum",
|
124
|
+
"elements",
|
125
|
+
"values",
|
126
|
+
"properties",
|
127
|
+
"optionalProperties",
|
128
|
+
"strict",
|
129
|
+
"discriminator",
|
130
|
+
"mapping",
|
131
|
+
"ref",
|
132
|
+
"definitions"
|
133
|
+
];
|
134
|
+
if (!isObject(input)) {
|
135
|
+
return false;
|
136
|
+
}
|
137
|
+
for (const key of Object.keys(input)) {
|
138
|
+
if (!allowedProperties.includes(key)) {
|
139
|
+
return false;
|
140
|
+
}
|
141
|
+
}
|
142
|
+
return true;
|
143
|
+
}
|
144
|
+
function isSchemaFormEmpty(input) {
|
145
|
+
if (!isObject(input)) {
|
146
|
+
return false;
|
147
|
+
}
|
148
|
+
const keys = Object.keys(input);
|
149
|
+
if (keys.length === 0) {
|
150
|
+
return true;
|
151
|
+
}
|
152
|
+
if (keys.length === 1 && "metadata" in input && typeof input.metadata === "object" && input.metadata !== null) {
|
153
|
+
return true;
|
154
|
+
}
|
155
|
+
return false;
|
156
|
+
}
|
157
|
+
const TypeValues = [
|
158
|
+
"boolean",
|
159
|
+
"float32",
|
160
|
+
"float64",
|
161
|
+
"int8",
|
162
|
+
"uint8",
|
163
|
+
"int16",
|
164
|
+
"uint16",
|
165
|
+
"int32",
|
166
|
+
"uint32",
|
167
|
+
"int64",
|
168
|
+
"uint64",
|
169
|
+
"string",
|
170
|
+
"timestamp"
|
171
|
+
];
|
172
|
+
function isSchemaFormType(input) {
|
173
|
+
if (!isObject(input)) {
|
174
|
+
return false;
|
175
|
+
}
|
176
|
+
return TypeValues.includes(input.type);
|
177
|
+
}
|
178
|
+
function isSchemaFormEnum(input) {
|
179
|
+
if (!isObject(input)) {
|
180
|
+
return false;
|
181
|
+
}
|
182
|
+
return Array.isArray(input.enum) && input.enum.every((val) => typeof val === "string");
|
183
|
+
}
|
184
|
+
function isSchemaFormElements(input) {
|
185
|
+
if (!isObject(input)) {
|
186
|
+
return false;
|
187
|
+
}
|
188
|
+
return "elements" in input;
|
189
|
+
}
|
190
|
+
function isSchemaFormProperties(input) {
|
191
|
+
if (!isObject(input)) {
|
192
|
+
return false;
|
193
|
+
}
|
194
|
+
if ("properties" in input && typeof input.properties === "object" && input.properties !== null) {
|
195
|
+
const keys = Object.keys(input.properties);
|
196
|
+
if (keys[0]) {
|
197
|
+
return isSchema(input.properties[keys[0]]);
|
198
|
+
}
|
199
|
+
return true;
|
200
|
+
}
|
201
|
+
return false;
|
202
|
+
}
|
203
|
+
function isSchemaFormValues(input) {
|
204
|
+
if (!isObject(input)) {
|
205
|
+
return false;
|
206
|
+
}
|
207
|
+
return "values" in input && isSchema(input.values);
|
208
|
+
}
|
209
|
+
function isSchemaFormDiscriminator(input) {
|
210
|
+
if (!isObject(input)) {
|
211
|
+
return false;
|
212
|
+
}
|
213
|
+
if ("discriminator" in input && typeof input.discriminator === "string" && input.discriminator.length > 0 && typeof input.mapping === "object" && input.mapping !== null) {
|
214
|
+
for (const key of Object.keys(input.mapping)) {
|
215
|
+
if (!isSchemaFormProperties(input.mapping[key])) {
|
216
|
+
return false;
|
217
|
+
}
|
218
|
+
}
|
219
|
+
return true;
|
220
|
+
}
|
221
|
+
return false;
|
222
|
+
}
|
223
|
+
function isSchemaFormRef(input) {
|
224
|
+
return typeof input === "object" && input !== null && "ref" in input && typeof input.ref === "string" && input.ref.length > 0;
|
225
|
+
}
|
226
|
+
|
227
|
+
exports.HttpMethodValues = HttpMethodValues;
|
228
|
+
exports.SCHEMA_VERSION = SCHEMA_VERSION;
|
229
|
+
exports.TypeValues = TypeValues;
|
230
|
+
exports.createAppDefinition = createAppDefinition;
|
231
|
+
exports.defineGeneratorPlugin = defineGeneratorPlugin;
|
232
|
+
exports.isAppDefinition = isAppDefinition;
|
233
|
+
exports.isHttpMethod = isHttpMethod;
|
234
|
+
exports.isObject = isObject;
|
235
|
+
exports.isRpcDefinition = isRpcDefinition;
|
236
|
+
exports.isRpcDefinitionBase = isRpcDefinitionBase;
|
237
|
+
exports.isRpcHttpMethod = isRpcHttpMethod;
|
238
|
+
exports.isSchema = isSchema;
|
239
|
+
exports.isSchemaFormDiscriminator = isSchemaFormDiscriminator;
|
240
|
+
exports.isSchemaFormElements = isSchemaFormElements;
|
241
|
+
exports.isSchemaFormEmpty = isSchemaFormEmpty;
|
242
|
+
exports.isSchemaFormEnum = isSchemaFormEnum;
|
243
|
+
exports.isSchemaFormProperties = isSchemaFormProperties;
|
244
|
+
exports.isSchemaFormRef = isSchemaFormRef;
|
245
|
+
exports.isSchemaFormType = isSchemaFormType;
|
246
|
+
exports.isSchemaFormValues = isSchemaFormValues;
|
247
|
+
exports.isServiceDefinition = isServiceDefinition;
|
package/dist/index.d.cts
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
declare function isObject(input: unknown): input is Record<any, any>;
|
2
|
+
interface SchemaMetadata {
|
3
|
+
id?: string;
|
4
|
+
description?: string;
|
5
|
+
isDeprecated?: boolean;
|
6
|
+
}
|
7
|
+
type Schema = SchemaFormEmpty | SchemaFormType | SchemaFormEnum | SchemaFormElements | SchemaFormProperties | SchemaFormValues | SchemaFormDiscriminator | SchemaFormRef;
|
8
|
+
declare function isSchema(input: unknown): input is Schema;
|
9
|
+
interface SchemaFormEmpty {
|
10
|
+
nullable?: boolean;
|
11
|
+
metadata?: SchemaMetadata;
|
12
|
+
}
|
13
|
+
declare function isSchemaFormEmpty(input: unknown): input is SchemaFormEmpty;
|
14
|
+
declare const TypeValues: readonly ["boolean", "float32", "float64", "int8", "uint8", "int16", "uint16", "int32", "uint32", "int64", "uint64", "string", "timestamp"];
|
15
|
+
type Type = (typeof TypeValues)[number];
|
16
|
+
interface SchemaFormType extends SchemaFormEmpty {
|
17
|
+
type: Type;
|
18
|
+
}
|
19
|
+
declare function isSchemaFormType(input: unknown): input is SchemaFormType;
|
20
|
+
interface SchemaFormEnum extends SchemaFormEmpty {
|
21
|
+
enum: string[];
|
22
|
+
}
|
23
|
+
declare function isSchemaFormEnum(input: unknown): input is SchemaFormEnum;
|
24
|
+
interface SchemaFormElements extends SchemaFormEmpty {
|
25
|
+
elements: Schema;
|
26
|
+
}
|
27
|
+
declare function isSchemaFormElements(input: unknown): input is SchemaFormElements;
|
28
|
+
interface SchemaFormProperties extends SchemaFormEmpty {
|
29
|
+
properties: Record<string, Schema>;
|
30
|
+
optionalProperties?: Record<string, Schema>;
|
31
|
+
strict?: boolean;
|
32
|
+
}
|
33
|
+
declare function isSchemaFormProperties(input: unknown): input is SchemaFormProperties;
|
34
|
+
interface SchemaFormValues extends SchemaFormEmpty {
|
35
|
+
values: Schema;
|
36
|
+
}
|
37
|
+
declare function isSchemaFormValues(input: unknown): input is SchemaFormValues;
|
38
|
+
interface SchemaFormDiscriminator extends SchemaFormEmpty {
|
39
|
+
discriminator: string;
|
40
|
+
mapping: Record<string, SchemaFormProperties>;
|
41
|
+
}
|
42
|
+
declare function isSchemaFormDiscriminator(input: unknown): input is SchemaFormDiscriminator;
|
43
|
+
interface SchemaFormRef extends SchemaFormEmpty {
|
44
|
+
ref: string;
|
45
|
+
}
|
46
|
+
declare function isSchemaFormRef(input: unknown): input is SchemaFormRef;
|
47
|
+
|
48
|
+
declare const HttpMethodValues: readonly ["get", "post", "put", "patch", "delete", "head"];
|
49
|
+
type HttpMethod = (typeof HttpMethodValues)[number];
|
50
|
+
type RpcHttpMethod = Exclude<HttpMethod, "head">;
|
51
|
+
declare const isHttpMethod: (input: any) => input is HttpMethod;
|
52
|
+
declare const isRpcHttpMethod: (input: any) => input is RpcHttpMethod;
|
53
|
+
declare const SCHEMA_VERSION = "0.0.7";
|
54
|
+
interface AppDefinition {
|
55
|
+
schemaVersion: typeof SCHEMA_VERSION;
|
56
|
+
info?: {
|
57
|
+
title?: string;
|
58
|
+
description?: string;
|
59
|
+
version?: string;
|
60
|
+
[key: string]: string | undefined;
|
61
|
+
};
|
62
|
+
externalDocs?: {
|
63
|
+
description?: string;
|
64
|
+
url: string;
|
65
|
+
};
|
66
|
+
procedures: Record<string, RpcDefinition<string>>;
|
67
|
+
definitions: Record<string, Schema>;
|
68
|
+
}
|
69
|
+
declare function isAppDefinition(input: unknown): input is AppDefinition;
|
70
|
+
interface RpcDefinitionBase<T = string> {
|
71
|
+
path: string;
|
72
|
+
params?: T;
|
73
|
+
response?: T;
|
74
|
+
description?: string;
|
75
|
+
isDeprecated?: boolean;
|
76
|
+
}
|
77
|
+
interface HttpRpcDefinition<T = string> extends RpcDefinitionBase<T> {
|
78
|
+
transport: "http";
|
79
|
+
method: RpcHttpMethod;
|
80
|
+
isEventStream?: boolean;
|
81
|
+
}
|
82
|
+
interface WsRpcDefinition<T = string> extends RpcDefinitionBase<T> {
|
83
|
+
transport: "ws";
|
84
|
+
}
|
85
|
+
interface CustomRpcDefinition<T = string> extends RpcDefinitionBase<T> {
|
86
|
+
transport: `custom:${string}`;
|
87
|
+
[key: string]: unknown;
|
88
|
+
}
|
89
|
+
type RpcDefinition<T = string> = HttpRpcDefinition<T> | WsRpcDefinition<T> | CustomRpcDefinition<T>;
|
90
|
+
declare function isRpcDefinitionBase(input: unknown): input is RpcDefinitionBase;
|
91
|
+
declare function isRpcDefinition(input: unknown): input is RpcDefinition;
|
92
|
+
interface ServiceDefinition {
|
93
|
+
[key: string]: RpcDefinition | ServiceDefinition;
|
94
|
+
}
|
95
|
+
declare function isServiceDefinition(input: any): input is ServiceDefinition;
|
96
|
+
interface Generator<TOptions extends Record<string, any> | undefined> {
|
97
|
+
run: (def: AppDefinition, isDevServer?: boolean) => any;
|
98
|
+
options: TOptions;
|
99
|
+
}
|
100
|
+
type GeneratorPlugin<TOptions extends Record<string, any> | undefined> = (options: TOptions) => Generator<TOptions>;
|
101
|
+
declare function defineGeneratorPlugin<TOptions extends Record<string, any> | undefined>(plugin: GeneratorPlugin<TOptions>): GeneratorPlugin<TOptions>;
|
102
|
+
type RpcDefinitionHelper = RpcDefinition<SchemaFormProperties | SchemaFormDiscriminator>;
|
103
|
+
type AppDefinitionHelper = Omit<AppDefinition, "procedures" | "definitions" | "schemaVersion"> & {
|
104
|
+
procedures: Record<string, RpcDefinitionHelper>;
|
105
|
+
definitions?: AppDefinition["definitions"];
|
106
|
+
};
|
107
|
+
declare function createAppDefinition(input: AppDefinitionHelper): AppDefinition;
|
108
|
+
|
109
|
+
export { type AppDefinition, type CustomRpcDefinition, type Generator, type GeneratorPlugin, type HttpMethod, HttpMethodValues, type HttpRpcDefinition, type RpcDefinition, type RpcDefinitionBase, type RpcHttpMethod, SCHEMA_VERSION, type Schema, type SchemaFormDiscriminator, type SchemaFormElements, type SchemaFormEmpty, type SchemaFormEnum, type SchemaFormProperties, type SchemaFormRef, type SchemaFormType, type SchemaFormValues, type SchemaMetadata, type ServiceDefinition, type Type, TypeValues, type WsRpcDefinition, createAppDefinition, defineGeneratorPlugin, isAppDefinition, isHttpMethod, isObject, isRpcDefinition, isRpcDefinitionBase, isRpcHttpMethod, isSchema, isSchemaFormDiscriminator, isSchemaFormElements, isSchemaFormEmpty, isSchemaFormEnum, isSchemaFormProperties, isSchemaFormRef, isSchemaFormType, isSchemaFormValues, isServiceDefinition };
|
package/dist/index.d.mts
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
declare function isObject(input: unknown): input is Record<any, any>;
|
2
|
+
interface SchemaMetadata {
|
3
|
+
id?: string;
|
4
|
+
description?: string;
|
5
|
+
isDeprecated?: boolean;
|
6
|
+
}
|
7
|
+
type Schema = SchemaFormEmpty | SchemaFormType | SchemaFormEnum | SchemaFormElements | SchemaFormProperties | SchemaFormValues | SchemaFormDiscriminator | SchemaFormRef;
|
8
|
+
declare function isSchema(input: unknown): input is Schema;
|
9
|
+
interface SchemaFormEmpty {
|
10
|
+
nullable?: boolean;
|
11
|
+
metadata?: SchemaMetadata;
|
12
|
+
}
|
13
|
+
declare function isSchemaFormEmpty(input: unknown): input is SchemaFormEmpty;
|
14
|
+
declare const TypeValues: readonly ["boolean", "float32", "float64", "int8", "uint8", "int16", "uint16", "int32", "uint32", "int64", "uint64", "string", "timestamp"];
|
15
|
+
type Type = (typeof TypeValues)[number];
|
16
|
+
interface SchemaFormType extends SchemaFormEmpty {
|
17
|
+
type: Type;
|
18
|
+
}
|
19
|
+
declare function isSchemaFormType(input: unknown): input is SchemaFormType;
|
20
|
+
interface SchemaFormEnum extends SchemaFormEmpty {
|
21
|
+
enum: string[];
|
22
|
+
}
|
23
|
+
declare function isSchemaFormEnum(input: unknown): input is SchemaFormEnum;
|
24
|
+
interface SchemaFormElements extends SchemaFormEmpty {
|
25
|
+
elements: Schema;
|
26
|
+
}
|
27
|
+
declare function isSchemaFormElements(input: unknown): input is SchemaFormElements;
|
28
|
+
interface SchemaFormProperties extends SchemaFormEmpty {
|
29
|
+
properties: Record<string, Schema>;
|
30
|
+
optionalProperties?: Record<string, Schema>;
|
31
|
+
strict?: boolean;
|
32
|
+
}
|
33
|
+
declare function isSchemaFormProperties(input: unknown): input is SchemaFormProperties;
|
34
|
+
interface SchemaFormValues extends SchemaFormEmpty {
|
35
|
+
values: Schema;
|
36
|
+
}
|
37
|
+
declare function isSchemaFormValues(input: unknown): input is SchemaFormValues;
|
38
|
+
interface SchemaFormDiscriminator extends SchemaFormEmpty {
|
39
|
+
discriminator: string;
|
40
|
+
mapping: Record<string, SchemaFormProperties>;
|
41
|
+
}
|
42
|
+
declare function isSchemaFormDiscriminator(input: unknown): input is SchemaFormDiscriminator;
|
43
|
+
interface SchemaFormRef extends SchemaFormEmpty {
|
44
|
+
ref: string;
|
45
|
+
}
|
46
|
+
declare function isSchemaFormRef(input: unknown): input is SchemaFormRef;
|
47
|
+
|
48
|
+
declare const HttpMethodValues: readonly ["get", "post", "put", "patch", "delete", "head"];
|
49
|
+
type HttpMethod = (typeof HttpMethodValues)[number];
|
50
|
+
type RpcHttpMethod = Exclude<HttpMethod, "head">;
|
51
|
+
declare const isHttpMethod: (input: any) => input is HttpMethod;
|
52
|
+
declare const isRpcHttpMethod: (input: any) => input is RpcHttpMethod;
|
53
|
+
declare const SCHEMA_VERSION = "0.0.7";
|
54
|
+
interface AppDefinition {
|
55
|
+
schemaVersion: typeof SCHEMA_VERSION;
|
56
|
+
info?: {
|
57
|
+
title?: string;
|
58
|
+
description?: string;
|
59
|
+
version?: string;
|
60
|
+
[key: string]: string | undefined;
|
61
|
+
};
|
62
|
+
externalDocs?: {
|
63
|
+
description?: string;
|
64
|
+
url: string;
|
65
|
+
};
|
66
|
+
procedures: Record<string, RpcDefinition<string>>;
|
67
|
+
definitions: Record<string, Schema>;
|
68
|
+
}
|
69
|
+
declare function isAppDefinition(input: unknown): input is AppDefinition;
|
70
|
+
interface RpcDefinitionBase<T = string> {
|
71
|
+
path: string;
|
72
|
+
params?: T;
|
73
|
+
response?: T;
|
74
|
+
description?: string;
|
75
|
+
isDeprecated?: boolean;
|
76
|
+
}
|
77
|
+
interface HttpRpcDefinition<T = string> extends RpcDefinitionBase<T> {
|
78
|
+
transport: "http";
|
79
|
+
method: RpcHttpMethod;
|
80
|
+
isEventStream?: boolean;
|
81
|
+
}
|
82
|
+
interface WsRpcDefinition<T = string> extends RpcDefinitionBase<T> {
|
83
|
+
transport: "ws";
|
84
|
+
}
|
85
|
+
interface CustomRpcDefinition<T = string> extends RpcDefinitionBase<T> {
|
86
|
+
transport: `custom:${string}`;
|
87
|
+
[key: string]: unknown;
|
88
|
+
}
|
89
|
+
type RpcDefinition<T = string> = HttpRpcDefinition<T> | WsRpcDefinition<T> | CustomRpcDefinition<T>;
|
90
|
+
declare function isRpcDefinitionBase(input: unknown): input is RpcDefinitionBase;
|
91
|
+
declare function isRpcDefinition(input: unknown): input is RpcDefinition;
|
92
|
+
interface ServiceDefinition {
|
93
|
+
[key: string]: RpcDefinition | ServiceDefinition;
|
94
|
+
}
|
95
|
+
declare function isServiceDefinition(input: any): input is ServiceDefinition;
|
96
|
+
interface Generator<TOptions extends Record<string, any> | undefined> {
|
97
|
+
run: (def: AppDefinition, isDevServer?: boolean) => any;
|
98
|
+
options: TOptions;
|
99
|
+
}
|
100
|
+
type GeneratorPlugin<TOptions extends Record<string, any> | undefined> = (options: TOptions) => Generator<TOptions>;
|
101
|
+
declare function defineGeneratorPlugin<TOptions extends Record<string, any> | undefined>(plugin: GeneratorPlugin<TOptions>): GeneratorPlugin<TOptions>;
|
102
|
+
type RpcDefinitionHelper = RpcDefinition<SchemaFormProperties | SchemaFormDiscriminator>;
|
103
|
+
type AppDefinitionHelper = Omit<AppDefinition, "procedures" | "definitions" | "schemaVersion"> & {
|
104
|
+
procedures: Record<string, RpcDefinitionHelper>;
|
105
|
+
definitions?: AppDefinition["definitions"];
|
106
|
+
};
|
107
|
+
declare function createAppDefinition(input: AppDefinitionHelper): AppDefinition;
|
108
|
+
|
109
|
+
export { type AppDefinition, type CustomRpcDefinition, type Generator, type GeneratorPlugin, type HttpMethod, HttpMethodValues, type HttpRpcDefinition, type RpcDefinition, type RpcDefinitionBase, type RpcHttpMethod, SCHEMA_VERSION, type Schema, type SchemaFormDiscriminator, type SchemaFormElements, type SchemaFormEmpty, type SchemaFormEnum, type SchemaFormProperties, type SchemaFormRef, type SchemaFormType, type SchemaFormValues, type SchemaMetadata, type ServiceDefinition, type Type, TypeValues, type WsRpcDefinition, createAppDefinition, defineGeneratorPlugin, isAppDefinition, isHttpMethod, isObject, isRpcDefinition, isRpcDefinitionBase, isRpcHttpMethod, isSchema, isSchemaFormDiscriminator, isSchemaFormElements, isSchemaFormEmpty, isSchemaFormEnum, isSchemaFormProperties, isSchemaFormRef, isSchemaFormType, isSchemaFormValues, isServiceDefinition };
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
declare function isObject(input: unknown): input is Record<any, any>;
|
2
|
+
interface SchemaMetadata {
|
3
|
+
id?: string;
|
4
|
+
description?: string;
|
5
|
+
isDeprecated?: boolean;
|
6
|
+
}
|
7
|
+
type Schema = SchemaFormEmpty | SchemaFormType | SchemaFormEnum | SchemaFormElements | SchemaFormProperties | SchemaFormValues | SchemaFormDiscriminator | SchemaFormRef;
|
8
|
+
declare function isSchema(input: unknown): input is Schema;
|
9
|
+
interface SchemaFormEmpty {
|
10
|
+
nullable?: boolean;
|
11
|
+
metadata?: SchemaMetadata;
|
12
|
+
}
|
13
|
+
declare function isSchemaFormEmpty(input: unknown): input is SchemaFormEmpty;
|
14
|
+
declare const TypeValues: readonly ["boolean", "float32", "float64", "int8", "uint8", "int16", "uint16", "int32", "uint32", "int64", "uint64", "string", "timestamp"];
|
15
|
+
type Type = (typeof TypeValues)[number];
|
16
|
+
interface SchemaFormType extends SchemaFormEmpty {
|
17
|
+
type: Type;
|
18
|
+
}
|
19
|
+
declare function isSchemaFormType(input: unknown): input is SchemaFormType;
|
20
|
+
interface SchemaFormEnum extends SchemaFormEmpty {
|
21
|
+
enum: string[];
|
22
|
+
}
|
23
|
+
declare function isSchemaFormEnum(input: unknown): input is SchemaFormEnum;
|
24
|
+
interface SchemaFormElements extends SchemaFormEmpty {
|
25
|
+
elements: Schema;
|
26
|
+
}
|
27
|
+
declare function isSchemaFormElements(input: unknown): input is SchemaFormElements;
|
28
|
+
interface SchemaFormProperties extends SchemaFormEmpty {
|
29
|
+
properties: Record<string, Schema>;
|
30
|
+
optionalProperties?: Record<string, Schema>;
|
31
|
+
strict?: boolean;
|
32
|
+
}
|
33
|
+
declare function isSchemaFormProperties(input: unknown): input is SchemaFormProperties;
|
34
|
+
interface SchemaFormValues extends SchemaFormEmpty {
|
35
|
+
values: Schema;
|
36
|
+
}
|
37
|
+
declare function isSchemaFormValues(input: unknown): input is SchemaFormValues;
|
38
|
+
interface SchemaFormDiscriminator extends SchemaFormEmpty {
|
39
|
+
discriminator: string;
|
40
|
+
mapping: Record<string, SchemaFormProperties>;
|
41
|
+
}
|
42
|
+
declare function isSchemaFormDiscriminator(input: unknown): input is SchemaFormDiscriminator;
|
43
|
+
interface SchemaFormRef extends SchemaFormEmpty {
|
44
|
+
ref: string;
|
45
|
+
}
|
46
|
+
declare function isSchemaFormRef(input: unknown): input is SchemaFormRef;
|
47
|
+
|
48
|
+
declare const HttpMethodValues: readonly ["get", "post", "put", "patch", "delete", "head"];
|
49
|
+
type HttpMethod = (typeof HttpMethodValues)[number];
|
50
|
+
type RpcHttpMethod = Exclude<HttpMethod, "head">;
|
51
|
+
declare const isHttpMethod: (input: any) => input is HttpMethod;
|
52
|
+
declare const isRpcHttpMethod: (input: any) => input is RpcHttpMethod;
|
53
|
+
declare const SCHEMA_VERSION = "0.0.7";
|
54
|
+
interface AppDefinition {
|
55
|
+
schemaVersion: typeof SCHEMA_VERSION;
|
56
|
+
info?: {
|
57
|
+
title?: string;
|
58
|
+
description?: string;
|
59
|
+
version?: string;
|
60
|
+
[key: string]: string | undefined;
|
61
|
+
};
|
62
|
+
externalDocs?: {
|
63
|
+
description?: string;
|
64
|
+
url: string;
|
65
|
+
};
|
66
|
+
procedures: Record<string, RpcDefinition<string>>;
|
67
|
+
definitions: Record<string, Schema>;
|
68
|
+
}
|
69
|
+
declare function isAppDefinition(input: unknown): input is AppDefinition;
|
70
|
+
interface RpcDefinitionBase<T = string> {
|
71
|
+
path: string;
|
72
|
+
params?: T;
|
73
|
+
response?: T;
|
74
|
+
description?: string;
|
75
|
+
isDeprecated?: boolean;
|
76
|
+
}
|
77
|
+
interface HttpRpcDefinition<T = string> extends RpcDefinitionBase<T> {
|
78
|
+
transport: "http";
|
79
|
+
method: RpcHttpMethod;
|
80
|
+
isEventStream?: boolean;
|
81
|
+
}
|
82
|
+
interface WsRpcDefinition<T = string> extends RpcDefinitionBase<T> {
|
83
|
+
transport: "ws";
|
84
|
+
}
|
85
|
+
interface CustomRpcDefinition<T = string> extends RpcDefinitionBase<T> {
|
86
|
+
transport: `custom:${string}`;
|
87
|
+
[key: string]: unknown;
|
88
|
+
}
|
89
|
+
type RpcDefinition<T = string> = HttpRpcDefinition<T> | WsRpcDefinition<T> | CustomRpcDefinition<T>;
|
90
|
+
declare function isRpcDefinitionBase(input: unknown): input is RpcDefinitionBase;
|
91
|
+
declare function isRpcDefinition(input: unknown): input is RpcDefinition;
|
92
|
+
interface ServiceDefinition {
|
93
|
+
[key: string]: RpcDefinition | ServiceDefinition;
|
94
|
+
}
|
95
|
+
declare function isServiceDefinition(input: any): input is ServiceDefinition;
|
96
|
+
interface Generator<TOptions extends Record<string, any> | undefined> {
|
97
|
+
run: (def: AppDefinition, isDevServer?: boolean) => any;
|
98
|
+
options: TOptions;
|
99
|
+
}
|
100
|
+
type GeneratorPlugin<TOptions extends Record<string, any> | undefined> = (options: TOptions) => Generator<TOptions>;
|
101
|
+
declare function defineGeneratorPlugin<TOptions extends Record<string, any> | undefined>(plugin: GeneratorPlugin<TOptions>): GeneratorPlugin<TOptions>;
|
102
|
+
type RpcDefinitionHelper = RpcDefinition<SchemaFormProperties | SchemaFormDiscriminator>;
|
103
|
+
type AppDefinitionHelper = Omit<AppDefinition, "procedures" | "definitions" | "schemaVersion"> & {
|
104
|
+
procedures: Record<string, RpcDefinitionHelper>;
|
105
|
+
definitions?: AppDefinition["definitions"];
|
106
|
+
};
|
107
|
+
declare function createAppDefinition(input: AppDefinitionHelper): AppDefinition;
|
108
|
+
|
109
|
+
export { type AppDefinition, type CustomRpcDefinition, type Generator, type GeneratorPlugin, type HttpMethod, HttpMethodValues, type HttpRpcDefinition, type RpcDefinition, type RpcDefinitionBase, type RpcHttpMethod, SCHEMA_VERSION, type Schema, type SchemaFormDiscriminator, type SchemaFormElements, type SchemaFormEmpty, type SchemaFormEnum, type SchemaFormProperties, type SchemaFormRef, type SchemaFormType, type SchemaFormValues, type SchemaMetadata, type ServiceDefinition, type Type, TypeValues, type WsRpcDefinition, createAppDefinition, defineGeneratorPlugin, isAppDefinition, isHttpMethod, isObject, isRpcDefinition, isRpcDefinitionBase, isRpcHttpMethod, isSchema, isSchemaFormDiscriminator, isSchemaFormElements, isSchemaFormEmpty, isSchemaFormEnum, isSchemaFormProperties, isSchemaFormRef, isSchemaFormType, isSchemaFormValues, isServiceDefinition };
|
package/dist/index.mjs
ADDED
@@ -0,0 +1,225 @@
|
|
1
|
+
import { pascalCase } from 'scule';
|
2
|
+
|
3
|
+
const HttpMethodValues = [
|
4
|
+
"get",
|
5
|
+
"post",
|
6
|
+
"put",
|
7
|
+
"patch",
|
8
|
+
"delete",
|
9
|
+
"head"
|
10
|
+
];
|
11
|
+
const isHttpMethod = (input) => {
|
12
|
+
if (typeof input !== "string") {
|
13
|
+
return false;
|
14
|
+
}
|
15
|
+
return HttpMethodValues.includes(input);
|
16
|
+
};
|
17
|
+
const isRpcHttpMethod = (input) => {
|
18
|
+
return isHttpMethod(input) && input !== "head";
|
19
|
+
};
|
20
|
+
const SCHEMA_VERSION = "0.0.7";
|
21
|
+
function isAppDefinition(input) {
|
22
|
+
if (typeof input !== "object") {
|
23
|
+
return false;
|
24
|
+
}
|
25
|
+
const inputObj = input;
|
26
|
+
if (typeof inputObj.schemaVersion !== "string") {
|
27
|
+
return false;
|
28
|
+
}
|
29
|
+
if (typeof inputObj.procedures !== "object") {
|
30
|
+
return false;
|
31
|
+
}
|
32
|
+
if (typeof inputObj.definitions !== "object") {
|
33
|
+
return false;
|
34
|
+
}
|
35
|
+
return true;
|
36
|
+
}
|
37
|
+
function isRpcDefinitionBase(input) {
|
38
|
+
if (typeof input !== "object" || input === null) {
|
39
|
+
return false;
|
40
|
+
}
|
41
|
+
if ("params" in input && typeof input.params !== "undefined" && typeof input.params !== "string") {
|
42
|
+
return false;
|
43
|
+
}
|
44
|
+
if ("response" in input && typeof input.response !== "undefined" && typeof input.response !== "string") {
|
45
|
+
return false;
|
46
|
+
}
|
47
|
+
return "transport" in input && typeof input.transport === "string" && input.transport.length > 0 && "path" in input && typeof input.path === "string" && input.path.length > 0;
|
48
|
+
}
|
49
|
+
function isRpcDefinition(input) {
|
50
|
+
if (!isRpcDefinitionBase(input)) {
|
51
|
+
return false;
|
52
|
+
}
|
53
|
+
if (!("transport" in input) || typeof input.transport !== "string") {
|
54
|
+
return false;
|
55
|
+
}
|
56
|
+
if (input.transport === "http") {
|
57
|
+
return "method" in input && isRpcHttpMethod(input.method);
|
58
|
+
}
|
59
|
+
if (input.transport === "ws") {
|
60
|
+
return true;
|
61
|
+
}
|
62
|
+
if (input.transport.startsWith("custom:")) {
|
63
|
+
return true;
|
64
|
+
}
|
65
|
+
return false;
|
66
|
+
}
|
67
|
+
function isServiceDefinition(input) {
|
68
|
+
if (typeof input !== "object") {
|
69
|
+
return false;
|
70
|
+
}
|
71
|
+
for (const key of Object.keys(input)) {
|
72
|
+
if (typeof input[key] !== "object") {
|
73
|
+
return false;
|
74
|
+
}
|
75
|
+
}
|
76
|
+
return true;
|
77
|
+
}
|
78
|
+
function defineGeneratorPlugin(plugin) {
|
79
|
+
return plugin;
|
80
|
+
}
|
81
|
+
function createAppDefinition(input) {
|
82
|
+
const definitions = { ...input.definitions };
|
83
|
+
const procedures = {};
|
84
|
+
for (const key of Object.keys(input.procedures)) {
|
85
|
+
const def = input.procedures[key];
|
86
|
+
let paramName;
|
87
|
+
if (def.params) {
|
88
|
+
paramName = def.params.metadata?.id ?? pascalCase(`${key.split(".").join("_")}Params`);
|
89
|
+
definitions[paramName] = def.params;
|
90
|
+
}
|
91
|
+
let responseName;
|
92
|
+
if (def.response) {
|
93
|
+
responseName = def.response.metadata?.id ?? pascalCase(`${key.split(".").join("_")}Response`);
|
94
|
+
definitions[responseName] = def.response;
|
95
|
+
}
|
96
|
+
delete def.params;
|
97
|
+
delete def.response;
|
98
|
+
procedures[key] = {
|
99
|
+
...def,
|
100
|
+
params: paramName,
|
101
|
+
response: responseName
|
102
|
+
};
|
103
|
+
}
|
104
|
+
const result = {
|
105
|
+
schemaVersion: "0.0.7",
|
106
|
+
...input,
|
107
|
+
procedures,
|
108
|
+
definitions
|
109
|
+
};
|
110
|
+
return result;
|
111
|
+
}
|
112
|
+
|
113
|
+
function isObject(input) {
|
114
|
+
return typeof input === "object" && input !== null;
|
115
|
+
}
|
116
|
+
function isSchema(input) {
|
117
|
+
const allowedProperties = [
|
118
|
+
"metadata",
|
119
|
+
"nullable",
|
120
|
+
"type",
|
121
|
+
"enum",
|
122
|
+
"elements",
|
123
|
+
"values",
|
124
|
+
"properties",
|
125
|
+
"optionalProperties",
|
126
|
+
"strict",
|
127
|
+
"discriminator",
|
128
|
+
"mapping",
|
129
|
+
"ref",
|
130
|
+
"definitions"
|
131
|
+
];
|
132
|
+
if (!isObject(input)) {
|
133
|
+
return false;
|
134
|
+
}
|
135
|
+
for (const key of Object.keys(input)) {
|
136
|
+
if (!allowedProperties.includes(key)) {
|
137
|
+
return false;
|
138
|
+
}
|
139
|
+
}
|
140
|
+
return true;
|
141
|
+
}
|
142
|
+
function isSchemaFormEmpty(input) {
|
143
|
+
if (!isObject(input)) {
|
144
|
+
return false;
|
145
|
+
}
|
146
|
+
const keys = Object.keys(input);
|
147
|
+
if (keys.length === 0) {
|
148
|
+
return true;
|
149
|
+
}
|
150
|
+
if (keys.length === 1 && "metadata" in input && typeof input.metadata === "object" && input.metadata !== null) {
|
151
|
+
return true;
|
152
|
+
}
|
153
|
+
return false;
|
154
|
+
}
|
155
|
+
const TypeValues = [
|
156
|
+
"boolean",
|
157
|
+
"float32",
|
158
|
+
"float64",
|
159
|
+
"int8",
|
160
|
+
"uint8",
|
161
|
+
"int16",
|
162
|
+
"uint16",
|
163
|
+
"int32",
|
164
|
+
"uint32",
|
165
|
+
"int64",
|
166
|
+
"uint64",
|
167
|
+
"string",
|
168
|
+
"timestamp"
|
169
|
+
];
|
170
|
+
function isSchemaFormType(input) {
|
171
|
+
if (!isObject(input)) {
|
172
|
+
return false;
|
173
|
+
}
|
174
|
+
return TypeValues.includes(input.type);
|
175
|
+
}
|
176
|
+
function isSchemaFormEnum(input) {
|
177
|
+
if (!isObject(input)) {
|
178
|
+
return false;
|
179
|
+
}
|
180
|
+
return Array.isArray(input.enum) && input.enum.every((val) => typeof val === "string");
|
181
|
+
}
|
182
|
+
function isSchemaFormElements(input) {
|
183
|
+
if (!isObject(input)) {
|
184
|
+
return false;
|
185
|
+
}
|
186
|
+
return "elements" in input;
|
187
|
+
}
|
188
|
+
function isSchemaFormProperties(input) {
|
189
|
+
if (!isObject(input)) {
|
190
|
+
return false;
|
191
|
+
}
|
192
|
+
if ("properties" in input && typeof input.properties === "object" && input.properties !== null) {
|
193
|
+
const keys = Object.keys(input.properties);
|
194
|
+
if (keys[0]) {
|
195
|
+
return isSchema(input.properties[keys[0]]);
|
196
|
+
}
|
197
|
+
return true;
|
198
|
+
}
|
199
|
+
return false;
|
200
|
+
}
|
201
|
+
function isSchemaFormValues(input) {
|
202
|
+
if (!isObject(input)) {
|
203
|
+
return false;
|
204
|
+
}
|
205
|
+
return "values" in input && isSchema(input.values);
|
206
|
+
}
|
207
|
+
function isSchemaFormDiscriminator(input) {
|
208
|
+
if (!isObject(input)) {
|
209
|
+
return false;
|
210
|
+
}
|
211
|
+
if ("discriminator" in input && typeof input.discriminator === "string" && input.discriminator.length > 0 && typeof input.mapping === "object" && input.mapping !== null) {
|
212
|
+
for (const key of Object.keys(input.mapping)) {
|
213
|
+
if (!isSchemaFormProperties(input.mapping[key])) {
|
214
|
+
return false;
|
215
|
+
}
|
216
|
+
}
|
217
|
+
return true;
|
218
|
+
}
|
219
|
+
return false;
|
220
|
+
}
|
221
|
+
function isSchemaFormRef(input) {
|
222
|
+
return typeof input === "object" && input !== null && "ref" in input && typeof input.ref === "string" && input.ref.length > 0;
|
223
|
+
}
|
224
|
+
|
225
|
+
export { HttpMethodValues, SCHEMA_VERSION, TypeValues, createAppDefinition, defineGeneratorPlugin, isAppDefinition, isHttpMethod, isObject, isRpcDefinition, isRpcDefinitionBase, isRpcHttpMethod, isSchema, isSchemaFormDiscriminator, isSchemaFormElements, isSchemaFormEmpty, isSchemaFormEnum, isSchemaFormProperties, isSchemaFormRef, isSchemaFormType, isSchemaFormValues, isServiceDefinition };
|
package/package.json
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
{
|
2
|
+
"name": "@arrirpc/type-defs",
|
3
|
+
"type": "module",
|
4
|
+
"license": "MIT",
|
5
|
+
"author": {
|
6
|
+
"name": "joshmossas",
|
7
|
+
"url": "https://github.com/joshmossas"
|
8
|
+
},
|
9
|
+
"bugs": {
|
10
|
+
"url": "https://github.com/modiimedia/arri/issues"
|
11
|
+
},
|
12
|
+
"repository": {
|
13
|
+
"type": "git",
|
14
|
+
"url": "https://github.com/modiimedia/arri.git",
|
15
|
+
"directory": "tooling/type-defs"
|
16
|
+
},
|
17
|
+
"main": "./dist/index.cjs",
|
18
|
+
"module": "./dist/index.mjs",
|
19
|
+
"types": "./dist/index.d.ts",
|
20
|
+
"files": [
|
21
|
+
"dist"
|
22
|
+
],
|
23
|
+
"version": "0.70.0",
|
24
|
+
"dependencies": {
|
25
|
+
"scule": "^1.3.0"
|
26
|
+
}
|
27
|
+
}
|