silgi 0.41.20 → 0.41.22
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.
- package/dist/cli/index.mjs +1 -1
- package/dist/core/index.d.mts +13 -19
- package/dist/core/index.mjs +30 -60
- package/dist/types/index.d.mts +17 -18
- package/package.json +1 -1
package/dist/cli/index.mjs
CHANGED
package/dist/core/index.d.mts
CHANGED
|
@@ -25,7 +25,7 @@ declare function middleware(event: SilgiEvent, url?: {
|
|
|
25
25
|
}): Promise<any>;
|
|
26
26
|
declare function handler(event: SilgiEvent, url?: {
|
|
27
27
|
path?: string;
|
|
28
|
-
method?: HTTPMethod;
|
|
28
|
+
method?: HTTPMethod | '';
|
|
29
29
|
}, input?: any): Promise<any>;
|
|
30
30
|
|
|
31
31
|
/**
|
|
@@ -158,25 +158,19 @@ declare function deepMergeObjects<T extends readonly Record<string, any>[]>(sche
|
|
|
158
158
|
* ServiceSetup tipinden oluşan bir yardımcı fonksiyon.
|
|
159
159
|
* Tip güvenliğini artırmak için ServiceSetup objesini doğrudan döndürür.
|
|
160
160
|
*/
|
|
161
|
-
declare function defineServiceSetup<Parent extends StandardSchemaV1, Input extends StandardSchemaV1 = StandardSchemaV1, Output extends StandardSchemaV1 = StandardSchemaV1, PathParams extends StandardSchemaV1 | undefined | never = undefined, QueryParams extends StandardSchemaV1 | undefined | never = undefined, Resolved extends boolean = false, HiddenParameters extends boolean = false>(setup: ServiceSetup<Parent, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>): ServiceSetup<Parent, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>;
|
|
162
|
-
type
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
CONNECT?: ServiceSetup<Parent, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>;
|
|
173
|
-
TRACE?: ServiceSetup<Parent, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>;
|
|
174
|
-
};
|
|
175
|
-
declare function createService<Parent extends StandardSchemaV1, Path extends keyof Routers = keyof Routers, Input extends StandardSchemaV1 = StandardSchemaV1, Output extends StandardSchemaV1 = StandardSchemaV1, PathParams extends StandardSchemaV1 | undefined | never = undefined, QueryParams extends StandardSchemaV1 | undefined | never = undefined, Resolved extends boolean = false, HiddenParameters extends boolean = false>(params: {
|
|
176
|
-
path: Max4Slashes<Path>;
|
|
177
|
-
} & ServiceSetupsForMethods<Parent, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>): {
|
|
178
|
-
[K in Path]: ServiceSetupsForMethods<Parent, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>;
|
|
161
|
+
declare function defineServiceSetup<Parent extends StandardSchemaV1, Path extends keyof Routers, Input extends StandardSchemaV1 = StandardSchemaV1, Output extends StandardSchemaV1 = StandardSchemaV1, PathParams extends StandardSchemaV1 | undefined | never = undefined, QueryParams extends StandardSchemaV1 | undefined | never = undefined, Resolved extends boolean = false, HiddenParameters extends boolean = false>(setup: ServiceSetup<Parent, Path, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>): ServiceSetup<Parent, Path, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>;
|
|
162
|
+
type ServiceSetupsForMethods<Parent extends StandardSchemaV1, Path extends keyof Routers, Input extends StandardSchemaV1 = StandardSchemaV1, Output extends StandardSchemaV1 = StandardSchemaV1, PathParams extends StandardSchemaV1 | undefined | never = undefined, QueryParams extends StandardSchemaV1 | undefined | never = undefined, Resolved extends boolean = false, HiddenParameters extends boolean = false> = {
|
|
163
|
+
GET?: ServiceSetup<Parent, Path, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>;
|
|
164
|
+
POST?: ServiceSetup<Parent, Path, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>;
|
|
165
|
+
PUT?: ServiceSetup<Parent, Path, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>;
|
|
166
|
+
PATCH?: ServiceSetup<Parent, Path, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>;
|
|
167
|
+
DELETE?: ServiceSetup<Parent, Path, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>;
|
|
168
|
+
OPTIONS?: ServiceSetup<Parent, Path, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>;
|
|
169
|
+
HEAD?: ServiceSetup<Parent, Path, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>;
|
|
170
|
+
CONNECT?: ServiceSetup<Parent, Path, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>;
|
|
171
|
+
TRACE?: ServiceSetup<Parent, Path, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>;
|
|
179
172
|
};
|
|
173
|
+
declare function createService<Parent extends StandardSchemaV1, Path extends keyof Routers, Input extends StandardSchemaV1 = StandardSchemaV1, Output extends StandardSchemaV1 = StandardSchemaV1, PathParams extends StandardSchemaV1 | undefined | never = undefined, QueryParams extends StandardSchemaV1 | undefined | never = undefined, Resolved extends boolean = false, HiddenParameters extends boolean = false>(params: ServiceSetupsForMethods<Parent, Path, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>): ServiceSetupsForMethods<Parent, Path, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>;
|
|
180
174
|
|
|
181
175
|
type WildcardVariants<Path extends string, Acc extends string = ''> = Path extends `${infer Head}/${infer Tail}` ? Tail extends '' ? `${Acc}${Head}` : `${Acc}${Head}/${Tail}` | `${Acc}${Head}/*` | `${Acc}${Head}/**` | WildcardVariants<Tail, `${Acc}${Head}/`> : `${Acc}${Path}`;
|
|
182
176
|
type MiddlewarePath<S extends WildcardVariants<keyof Routers>> = S | 'global';
|
package/dist/core/index.mjs
CHANGED
|
@@ -298,39 +298,39 @@ async function createSilgi(config) {
|
|
|
298
298
|
if (!silgi.router) {
|
|
299
299
|
silgi.router = createRouter();
|
|
300
300
|
}
|
|
301
|
-
for (const [
|
|
302
|
-
|
|
301
|
+
for (const [method, object] of Object.entries(silgi.services)) {
|
|
302
|
+
if (!object) continue;
|
|
303
|
+
const path = String(object.path);
|
|
304
|
+
const routeParts = path.split("/").filter(Boolean);
|
|
303
305
|
if (routeParts.length > 0) {
|
|
304
306
|
const prefix = `/${routeParts[0]}`;
|
|
305
307
|
if (!silgi.routerPrefixs.includes(prefix)) {
|
|
306
308
|
silgi.routerPrefixs.push(prefix);
|
|
307
309
|
}
|
|
308
310
|
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
routeWithParams += `/:${param}`;
|
|
323
|
-
}
|
|
311
|
+
let routeWithParams = path;
|
|
312
|
+
if (object.pathParams) {
|
|
313
|
+
const jsonSchema = await toJsonSchema(object.pathParams);
|
|
314
|
+
if (jsonSchema && jsonSchema.properties) {
|
|
315
|
+
const paramNames = Object.keys(jsonSchema.properties);
|
|
316
|
+
if (paramNames.length > 0) {
|
|
317
|
+
const routeParts2 = routeWithParams.split("/");
|
|
318
|
+
const lastParts = routeParts2.slice(-paramNames.length);
|
|
319
|
+
const alreadyHasParams = paramNames.every((name, idx) => lastParts[idx] === `:${name}`);
|
|
320
|
+
if (!alreadyHasParams) {
|
|
321
|
+
routeWithParams = routeWithParams.replace(/\/+$/, "");
|
|
322
|
+
for (const param of paramNames) {
|
|
323
|
+
routeWithParams += `/:${param}`;
|
|
324
324
|
}
|
|
325
325
|
}
|
|
326
326
|
}
|
|
327
327
|
}
|
|
328
|
-
addRoute(silgi.router, method, routeWithParams, {
|
|
329
|
-
method,
|
|
330
|
-
route: routeWithParams,
|
|
331
|
-
service: routeObject
|
|
332
|
-
});
|
|
333
328
|
}
|
|
329
|
+
addRoute(silgi.router, object.apiType === "graphql" ? "" : method, routeWithParams, {
|
|
330
|
+
method: object.apiType === "graphql" ? "" : method,
|
|
331
|
+
route: routeWithParams,
|
|
332
|
+
service: object
|
|
333
|
+
});
|
|
334
334
|
}
|
|
335
335
|
if (!silgi._middlewareRouter) {
|
|
336
336
|
silgi._middlewareRouter = createRouter();
|
|
@@ -353,14 +353,6 @@ async function createSilgi(config) {
|
|
|
353
353
|
continue;
|
|
354
354
|
}
|
|
355
355
|
const methods = routeObject.methods?.length ? routeObject.methods : ["ALL"];
|
|
356
|
-
if (methods.includes("GRAPHQL")) {
|
|
357
|
-
addRoute(silgi._middlewareRouter, "GRAPHQL", _route, {
|
|
358
|
-
method: "GRAPHQL",
|
|
359
|
-
route: _route,
|
|
360
|
-
middleware: routeObject.setup
|
|
361
|
-
});
|
|
362
|
-
continue;
|
|
363
|
-
}
|
|
364
356
|
for (const method of methods) {
|
|
365
357
|
const globalMethod = method === "ALL" ? "" : method.toUpperCase();
|
|
366
358
|
addRoute(silgi._middlewareRouter, globalMethod, _route, {
|
|
@@ -399,16 +391,13 @@ class _SilgiEvent {
|
|
|
399
391
|
req;
|
|
400
392
|
url;
|
|
401
393
|
context;
|
|
402
|
-
protocol;
|
|
403
|
-
// Added property
|
|
404
394
|
_res;
|
|
405
395
|
constructor(req, context) {
|
|
406
396
|
this.context = context || new EmptyObject();
|
|
397
|
+
this.context.protocol = "HTTP";
|
|
407
398
|
this.req = req;
|
|
408
399
|
const _url = req._url;
|
|
409
400
|
this.url = _url && _url instanceof URL ? _url : new FastURL(req.url);
|
|
410
|
-
let proto = this.url.protocol.replace(":", "");
|
|
411
|
-
this.protocol = proto === "https" ? "http" : proto;
|
|
412
401
|
}
|
|
413
402
|
get res() {
|
|
414
403
|
if (!this._res) {
|
|
@@ -609,7 +598,7 @@ function getUrlPrefix(path, method) {
|
|
|
609
598
|
|
|
610
599
|
async function orchestrate(route, event, _input) {
|
|
611
600
|
const silgiCtx = useSilgi();
|
|
612
|
-
const isGraphQL =
|
|
601
|
+
const isGraphQL = event.context.protocol === "GRAPHQL";
|
|
613
602
|
const silgiURL = !isGraphQL ? getUrlPrefix(route.route || event.req.url, route.method) : {
|
|
614
603
|
methodName: route.method || "GET",
|
|
615
604
|
namespaceName: "graphql",
|
|
@@ -635,7 +624,7 @@ async function orchestrate(route, event, _input) {
|
|
|
635
624
|
path: routerParams
|
|
636
625
|
};
|
|
637
626
|
try {
|
|
638
|
-
if (!route.service &&
|
|
627
|
+
if (!route.service && !isGraphQL) {
|
|
639
628
|
throw createError({
|
|
640
629
|
statusCode: 404,
|
|
641
630
|
statusMessage: "Service not found"
|
|
@@ -678,7 +667,7 @@ async function orchestrate(route, event, _input) {
|
|
|
678
667
|
// event
|
|
679
668
|
event,
|
|
680
669
|
// parent - graphql
|
|
681
|
-
|
|
670
|
+
void 0,
|
|
682
671
|
// info - graphql
|
|
683
672
|
{}
|
|
684
673
|
);
|
|
@@ -1020,6 +1009,8 @@ async function middleware(event, url) {
|
|
|
1020
1009
|
return _chain;
|
|
1021
1010
|
}
|
|
1022
1011
|
async function handler(event, url, input) {
|
|
1012
|
+
url ??= {};
|
|
1013
|
+
url.method = event.context.protocol === "GRAPHQL" ? "" : url.method || event.req.method;
|
|
1023
1014
|
const silgiCtx = useSilgi();
|
|
1024
1015
|
const pathname = url?.path || event.url.pathname;
|
|
1025
1016
|
let _chain = void 0;
|
|
@@ -1027,17 +1018,7 @@ async function handler(event, url, input) {
|
|
|
1027
1018
|
const data = middleware(event, url);
|
|
1028
1019
|
_chain = data;
|
|
1029
1020
|
if (silgiCtx.router) {
|
|
1030
|
-
let match = findRoute(silgiCtx.router, url
|
|
1031
|
-
if (!match && url?.method === "GRAPHQL") {
|
|
1032
|
-
match = {
|
|
1033
|
-
data: {
|
|
1034
|
-
method: url?.method || event.req.method,
|
|
1035
|
-
route: url?.path,
|
|
1036
|
-
service: {}
|
|
1037
|
-
},
|
|
1038
|
-
params: {}
|
|
1039
|
-
};
|
|
1040
|
-
}
|
|
1021
|
+
let match = findRoute(silgiCtx.router, url.method, pathname);
|
|
1041
1022
|
if (match) {
|
|
1042
1023
|
if (_chain) {
|
|
1043
1024
|
return _chain.then(async (_previous) => {
|
|
@@ -1108,18 +1089,7 @@ function defineServiceSetup(setup) {
|
|
|
1108
1089
|
return setup;
|
|
1109
1090
|
}
|
|
1110
1091
|
function createService(params) {
|
|
1111
|
-
|
|
1112
|
-
if (slashCount > 4) {
|
|
1113
|
-
throw new Error(
|
|
1114
|
-
`Path '${params.path}' is invalid: maximum 4 '/' allowed (found ${slashCount})`
|
|
1115
|
-
);
|
|
1116
|
-
}
|
|
1117
|
-
const { path, ...objects } = params;
|
|
1118
|
-
return {
|
|
1119
|
-
[path]: {
|
|
1120
|
-
...objects
|
|
1121
|
-
}
|
|
1122
|
-
};
|
|
1092
|
+
return params;
|
|
1123
1093
|
}
|
|
1124
1094
|
|
|
1125
1095
|
function createMiddleware(params) {
|
package/dist/types/index.d.mts
CHANGED
|
@@ -88,6 +88,7 @@ interface ExtendContext {
|
|
|
88
88
|
}
|
|
89
89
|
interface SilgiRuntimeContext extends Record<string, any> {
|
|
90
90
|
params?: Record<string, string>;
|
|
91
|
+
protocol: EventProtocol;
|
|
91
92
|
/**
|
|
92
93
|
* Matched router Node
|
|
93
94
|
*
|
|
@@ -105,10 +106,6 @@ type EventProtocol = 'GRAPHQL' | 'HTTP' | 'WEBSOCKET';
|
|
|
105
106
|
* Bu nitrojs, h3 event or request context.
|
|
106
107
|
*/
|
|
107
108
|
interface SilgiEvent extends Record<string, unknown> {
|
|
108
|
-
/**
|
|
109
|
-
* Protocol Type
|
|
110
|
-
*/
|
|
111
|
-
readonly protocol: EventProtocol;
|
|
112
109
|
/**
|
|
113
110
|
* Event context.
|
|
114
111
|
*/
|
|
@@ -230,6 +227,8 @@ interface ExtendShared {
|
|
|
230
227
|
|
|
231
228
|
interface ServicesObject {
|
|
232
229
|
}
|
|
230
|
+
type SlashCount<S extends string, Count extends any[] = []> = S extends `${infer _Prefix}/${infer Rest}` ? SlashCount<Rest, [any, ...Count]> : Count['length'];
|
|
231
|
+
type Max4Slashes<S extends keyof Routers> = SlashCount<S> extends 0 | 1 | 2 | 3 ? S : never;
|
|
233
232
|
/**
|
|
234
233
|
* Yardımcı tipler
|
|
235
234
|
*/
|
|
@@ -270,7 +269,9 @@ type ServiceHandler<Parent extends StandardSchemaV1, Input extends StandardSchem
|
|
|
270
269
|
/**
|
|
271
270
|
* Servis setup tipi
|
|
272
271
|
*/
|
|
273
|
-
interface ServiceSetup<Parent extends StandardSchemaV1, Input extends StandardSchemaV1 = StandardSchemaV1, Output extends StandardSchemaV1 = StandardSchemaV1, PathParams extends StandardSchemaV1 | undefined | never = undefined, QueryParams extends StandardSchemaV1 | undefined | never = undefined, Resolved extends boolean = false, HiddenParameters extends boolean = false> {
|
|
272
|
+
interface ServiceSetup<Parent extends StandardSchemaV1 = StandardSchemaV1, Path extends keyof Routers = keyof Routers, Input extends StandardSchemaV1 = StandardSchemaV1, Output extends StandardSchemaV1 = StandardSchemaV1, PathParams extends StandardSchemaV1 | undefined | never = undefined, QueryParams extends StandardSchemaV1 | undefined | never = undefined, Resolved extends boolean = false, HiddenParameters extends boolean = false> {
|
|
273
|
+
path: Max4Slashes<Path>;
|
|
274
|
+
apiType: 'rest' | 'graphql';
|
|
274
275
|
handler?: ServiceHandler<Parent, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>;
|
|
275
276
|
rules?: MergeRouteRules;
|
|
276
277
|
modules?: Partial<SetupModuleOption>;
|
|
@@ -290,17 +291,15 @@ interface ServiceSetup<Parent extends StandardSchemaV1, Input extends StandardSc
|
|
|
290
291
|
* Format: "routePath" => setup + methods + input + output + queryParams
|
|
291
292
|
*/
|
|
292
293
|
interface ResolvedServiceDefinition {
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
TRACE?: ServiceSetup<any, any, any, any, any, any>;
|
|
303
|
-
};
|
|
294
|
+
GET?: ServiceSetup;
|
|
295
|
+
POST?: ServiceSetup;
|
|
296
|
+
PUT?: ServiceSetup;
|
|
297
|
+
PATCH?: ServiceSetup;
|
|
298
|
+
DELETE?: ServiceSetup;
|
|
299
|
+
OPTIONS?: ServiceSetup;
|
|
300
|
+
HEAD?: ServiceSetup;
|
|
301
|
+
CONNECT?: ServiceSetup;
|
|
302
|
+
TRACE?: ServiceSetup;
|
|
304
303
|
}
|
|
305
304
|
/**
|
|
306
305
|
* SilgiURL tipi
|
|
@@ -699,13 +698,13 @@ interface ResolvedSchemaDefinition {
|
|
|
699
698
|
}
|
|
700
699
|
|
|
701
700
|
type StandardHTTPMethod = 'GET' | 'HEAD' | 'PATCH' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE';
|
|
702
|
-
type HTTPMethod = StandardHTTPMethod
|
|
701
|
+
type HTTPMethod = StandardHTTPMethod;
|
|
703
702
|
interface MetaData extends Record<string, unknown> {
|
|
704
703
|
}
|
|
705
704
|
interface SilgiRoute {
|
|
706
705
|
route?: string;
|
|
707
706
|
method?: HTTPMethod;
|
|
708
|
-
service?: ResolvedServiceDefinition[
|
|
707
|
+
service?: ResolvedServiceDefinition[keyof ResolvedServiceDefinition];
|
|
709
708
|
middleware?: MiddlewareSetup;
|
|
710
709
|
}
|
|
711
710
|
interface Silgi {
|