silgi 0.41.21 → 0.41.23
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 +29 -56
- package/dist/types/index.d.mts +16 -14
- 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 = 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> = {
|
|
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 = 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: 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, {
|
|
@@ -606,7 +598,7 @@ function getUrlPrefix(path, method) {
|
|
|
606
598
|
|
|
607
599
|
async function orchestrate(route, event, _input) {
|
|
608
600
|
const silgiCtx = useSilgi();
|
|
609
|
-
const isGraphQL =
|
|
601
|
+
const isGraphQL = event.context.protocol === "GRAPHQL";
|
|
610
602
|
const silgiURL = !isGraphQL ? getUrlPrefix(route.route || event.req.url, route.method) : {
|
|
611
603
|
methodName: route.method || "GET",
|
|
612
604
|
namespaceName: "graphql",
|
|
@@ -632,7 +624,7 @@ async function orchestrate(route, event, _input) {
|
|
|
632
624
|
path: routerParams
|
|
633
625
|
};
|
|
634
626
|
try {
|
|
635
|
-
if (!route.service &&
|
|
627
|
+
if (!route.service && !isGraphQL) {
|
|
636
628
|
throw createError({
|
|
637
629
|
statusCode: 404,
|
|
638
630
|
statusMessage: "Service not found"
|
|
@@ -675,7 +667,7 @@ async function orchestrate(route, event, _input) {
|
|
|
675
667
|
// event
|
|
676
668
|
event,
|
|
677
669
|
// parent - graphql
|
|
678
|
-
|
|
670
|
+
void 0,
|
|
679
671
|
// info - graphql
|
|
680
672
|
{}
|
|
681
673
|
);
|
|
@@ -1017,6 +1009,8 @@ async function middleware(event, url) {
|
|
|
1017
1009
|
return _chain;
|
|
1018
1010
|
}
|
|
1019
1011
|
async function handler(event, url, input) {
|
|
1012
|
+
url ??= {};
|
|
1013
|
+
url.method = event.context.protocol === "GRAPHQL" ? "" : url.method || event.req.method;
|
|
1020
1014
|
const silgiCtx = useSilgi();
|
|
1021
1015
|
const pathname = url?.path || event.url.pathname;
|
|
1022
1016
|
let _chain = void 0;
|
|
@@ -1024,17 +1018,7 @@ async function handler(event, url, input) {
|
|
|
1024
1018
|
const data = middleware(event, url);
|
|
1025
1019
|
_chain = data;
|
|
1026
1020
|
if (silgiCtx.router) {
|
|
1027
|
-
let match = findRoute(silgiCtx.router, url
|
|
1028
|
-
if (!match && url?.method === "GRAPHQL") {
|
|
1029
|
-
match = {
|
|
1030
|
-
data: {
|
|
1031
|
-
method: url?.method || event.req.method,
|
|
1032
|
-
route: url?.path,
|
|
1033
|
-
service: {}
|
|
1034
|
-
},
|
|
1035
|
-
params: {}
|
|
1036
|
-
};
|
|
1037
|
-
}
|
|
1021
|
+
let match = findRoute(silgiCtx.router, url.method, pathname);
|
|
1038
1022
|
if (match) {
|
|
1039
1023
|
if (_chain) {
|
|
1040
1024
|
return _chain.then(async (_previous) => {
|
|
@@ -1105,18 +1089,7 @@ function defineServiceSetup(setup) {
|
|
|
1105
1089
|
return setup;
|
|
1106
1090
|
}
|
|
1107
1091
|
function createService(params) {
|
|
1108
|
-
|
|
1109
|
-
if (slashCount > 4) {
|
|
1110
|
-
throw new Error(
|
|
1111
|
-
`Path '${params.path}' is invalid: maximum 4 '/' allowed (found ${slashCount})`
|
|
1112
|
-
);
|
|
1113
|
-
}
|
|
1114
|
-
const { path, ...objects } = params;
|
|
1115
|
-
return {
|
|
1116
|
-
[path]: {
|
|
1117
|
-
...objects
|
|
1118
|
-
}
|
|
1119
|
-
};
|
|
1092
|
+
return params;
|
|
1120
1093
|
}
|
|
1121
1094
|
|
|
1122
1095
|
function createMiddleware(params) {
|
package/dist/types/index.d.mts
CHANGED
|
@@ -227,6 +227,8 @@ interface ExtendShared {
|
|
|
227
227
|
|
|
228
228
|
interface ServicesObject {
|
|
229
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;
|
|
230
232
|
/**
|
|
231
233
|
* Yardımcı tipler
|
|
232
234
|
*/
|
|
@@ -267,7 +269,9 @@ type ServiceHandler<Parent extends StandardSchemaV1, Input extends StandardSchem
|
|
|
267
269
|
/**
|
|
268
270
|
* Servis setup tipi
|
|
269
271
|
*/
|
|
270
|
-
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';
|
|
271
275
|
handler?: ServiceHandler<Parent, Input, Output, PathParams, QueryParams, Resolved, HiddenParameters>;
|
|
272
276
|
rules?: MergeRouteRules;
|
|
273
277
|
modules?: Partial<SetupModuleOption>;
|
|
@@ -287,17 +291,15 @@ interface ServiceSetup<Parent extends StandardSchemaV1, Input extends StandardSc
|
|
|
287
291
|
* Format: "routePath" => setup + methods + input + output + queryParams
|
|
288
292
|
*/
|
|
289
293
|
interface ResolvedServiceDefinition {
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
TRACE?: ServiceSetup<any, any, any, any, any, any>;
|
|
300
|
-
};
|
|
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;
|
|
301
303
|
}
|
|
302
304
|
/**
|
|
303
305
|
* SilgiURL tipi
|
|
@@ -696,13 +698,13 @@ interface ResolvedSchemaDefinition {
|
|
|
696
698
|
}
|
|
697
699
|
|
|
698
700
|
type StandardHTTPMethod = 'GET' | 'HEAD' | 'PATCH' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE';
|
|
699
|
-
type HTTPMethod = StandardHTTPMethod
|
|
701
|
+
type HTTPMethod = StandardHTTPMethod;
|
|
700
702
|
interface MetaData extends Record<string, unknown> {
|
|
701
703
|
}
|
|
702
704
|
interface SilgiRoute {
|
|
703
705
|
route?: string;
|
|
704
706
|
method?: HTTPMethod;
|
|
705
|
-
service?: ResolvedServiceDefinition[
|
|
707
|
+
service?: ResolvedServiceDefinition[keyof ResolvedServiceDefinition];
|
|
706
708
|
middleware?: MiddlewareSetup;
|
|
707
709
|
}
|
|
708
710
|
interface Silgi {
|