silgi 0.4.8 → 0.4.11

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.
@@ -1,6 +1,6 @@
1
1
  const name = "silgi";
2
2
  const type = "module";
3
- const version = "0.4.8";
3
+ const version = "0.4.11";
4
4
  const packageManager = "pnpm@9.15.1";
5
5
  const sideEffects = false;
6
6
  const exports = {
@@ -250,10 +250,29 @@ async function generateRouterDTS(silgi) {
250
250
  silgi.hook("close", async () => {
251
251
  const { object } = await data();
252
252
  const uris = object.uris;
253
- const tag = "srn";
254
- const groupedRoutes = Object.entries(uris || {}).reduce((acc, [key, _value]) => {
253
+ const groupedPaths = /* @__PURE__ */ new Map();
254
+ Object.entries(uris || {}).forEach(([key, params]) => {
255
255
  const [service, resource, method, action] = key.split("/");
256
- const routePath = `${tag}/${service}/${resource}/${action}/${_value}`;
256
+ const basePath = params ? `${service}/${resource}/${action}/${params}` : `${service}/${resource}/${action}`;
257
+ const fullPath = `${service}/${resource}/${action}`;
258
+ if (!groupedPaths.has(basePath)) {
259
+ groupedPaths.set(basePath, /* @__PURE__ */ new Map());
260
+ }
261
+ groupedPaths.get(basePath)?.set(method.toLowerCase(), fullPath);
262
+ });
263
+ const keys = [
264
+ " keys: {",
265
+ Array.from(groupedPaths.entries()).map(([basePath, methods]) => {
266
+ return ` '/${basePath}': {${Array.from(methods.entries()).map(([method, path]) => `
267
+ ${method}: '/${path}'`).join(",")}
268
+ }`;
269
+ }).join(",\n"),
270
+ " }",
271
+ ""
272
+ ].join("\n");
273
+ const groupedRoutes = Object.entries(uris || {}).reduce((acc, [key, _params]) => {
274
+ const [service, resource, method, action] = key.split("/");
275
+ const routePath = `${service}/${resource}/${action}`;
257
276
  if (!acc[routePath]) {
258
277
  acc[routePath] = {};
259
278
  }
@@ -281,11 +300,16 @@ ${methodEntries}
281
300
  " interface InternalApi extends RouterTypes {}",
282
301
  "}"
283
302
  ];
303
+ const content = [
304
+ keys.slice(0, -1),
305
+ // son satırdaki boş satırı kaldır
306
+ ...routerTypes
307
+ ].join(",\n");
284
308
  const context = [
285
309
  "import type { ExtractInputFromURI, ExtractOutputFromURI, ExtractRouterParamsFromURI } from 'silgi/types'",
286
310
  "",
287
311
  "export interface RouterTypes {",
288
- routerTypes.join(",\n"),
312
+ content,
289
313
  "}",
290
314
  "",
291
315
  "declare module 'silgi/types' {",
@@ -14,49 +14,34 @@ declare function silgiGenerateType(silgi: SilgiCLI): Promise<{
14
14
 
15
15
  declare function createSilgi(config: SilgiConfig): Promise<Silgi>;
16
16
 
17
- type StaticRoutes = keyof SilgiRouterTypes;
18
- type ExtractStaticPath<T extends string> = T extends `${infer Start}/:${string}` ? Start : T;
19
- type RouteWithParams<T extends string> = T | `${T}/${string}`;
20
- type ValidRoute = StaticRoutes | RouteWithParams<ExtractStaticPath<StaticRoutes & string>>;
21
- type FindMatchingRoute<T extends string> = {
22
- [K in StaticRoutes]: ExtractStaticPath<K & string> extends never ? never : T extends `${ExtractStaticPath<K & string>}${string}` ? K : never;
23
- }[StaticRoutes];
24
- type Method<R extends ValidRoute> = R extends StaticRoutes ? keyof SilgiRouterTypes[R] : keyof SilgiRouterTypes[FindMatchingRoute<R & string>];
25
- type DefaultMethodType = 'get' | 'post' | 'put' | 'delete';
26
- type FetchResponseData<R extends ValidRoute, M extends Method<R>> = R extends StaticRoutes ? SilgiRouterTypes[R][M & keyof SilgiRouterTypes[R]]['output'] : SilgiRouterTypes[FindMatchingRoute<R & string>][M & keyof SilgiRouterTypes[FindMatchingRoute<R & string>]]['output'];
27
- type RequestBodyOption<R extends ValidRoute, M extends Method<R>> = R extends StaticRoutes ? SilgiRouterTypes[R][M & keyof SilgiRouterTypes[R]] extends {
28
- input: infer I;
29
- } ? {
30
- body: I;
31
- } : {
32
- body?: never;
33
- } : SilgiRouterTypes[FindMatchingRoute<R & string>][M & keyof SilgiRouterTypes[FindMatchingRoute<R & string>]] extends {
34
- input: infer I;
35
- } ? {
36
- body: I;
37
- } : {
38
- body?: never;
39
- };
40
- type ExtractParams<T extends string> = T extends `${string}:${infer Param}/${infer Rest}` ? {
17
+ type TrimAfterFourSlashes<T extends string> = T extends `/${infer S1}/${infer S2}/${infer S3}/${infer _}` ? `/${S1}/${S2}/${S3}` : T;
18
+ type AllPaths = SilgiRouterTypes extends {
19
+ keys: infer U;
20
+ } ? keyof U extends never ? string : keyof U : string;
21
+ type ExtractPathParams<T extends string> = T extends `${infer _Start}:${infer Param}/${infer Rest}` ? {
41
22
  [K in Param]: string;
42
- } & ExtractParams<Rest> : T extends `${string}:${infer Param}` ? {
23
+ } & ExtractPathParams<Rest> : T extends `${infer _Start}:${infer Param}` ? {
43
24
  [K in Param]: string;
44
25
  } : object;
45
- type RouterParams<R extends ValidRoute, M extends Method<R>> = R extends StaticRoutes ? SilgiRouterTypes[R][M & keyof SilgiRouterTypes[R]] extends {
46
- params: infer P;
26
+ type ValidRoute = AllPaths | (string & {});
27
+ type Method<R extends ValidRoute> = TrimAfterFourSlashes<R> extends keyof SilgiRouterTypes ? keyof SilgiRouterTypes[TrimAfterFourSlashes<R>] : never;
28
+ type FetchResponseData<R extends ValidRoute, M extends Method<R>> = TrimAfterFourSlashes<R> extends keyof SilgiRouterTypes ? SilgiRouterTypes[TrimAfterFourSlashes<R>][M]['output'] : never;
29
+ type RequestBodyOption<R extends ValidRoute, M extends Method<R>> = TrimAfterFourSlashes<R> extends keyof SilgiRouterTypes ? SilgiRouterTypes[TrimAfterFourSlashes<R>][M] extends {
30
+ input: infer I;
47
31
  } ? {
48
- params: P;
49
- } : R extends `${string}:${string}` ? {
50
- params: ExtractParams<R>;
51
- } : {
52
- params?: never;
32
+ body: I;
53
33
  } : {
54
- params: ExtractParams<FindMatchingRoute<R & string>>;
55
- };
34
+ body?: never;
35
+ } : never;
36
+ type RouterParams<R extends ValidRoute> = ExtractPathParams<R>;
56
37
  type SilgiFetchOptions<R extends ValidRoute, M extends Method<R>> = {
57
38
  method: M;
58
- } & RequestBodyOption<R, M> & RouterParams<R, M> & Omit<FetchOptions, 'query' | 'body' | 'method'>;
59
- type SilgiFetchClient = <R extends ValidRoute, M extends Method<R> | DefaultMethodType = 'get'>(url: R, options: SilgiFetchOptions<R, M extends Method<R> ? M : Extract<Method<R>, DefaultMethodType>>) => Promise<FetchResponseData<R, M extends Method<R> ? M : Extract<Method<R>, DefaultMethodType>>>;
39
+ } & RequestBodyOption<R, M> & (RouterParams<R> extends object ? {
40
+ params: RouterParams<R>;
41
+ } : {
42
+ params?: never;
43
+ }) & Omit<FetchOptions, 'query' | 'body' | 'method'>;
44
+ type SilgiFetchClient = <R extends ValidRoute, M extends Method<R> = 'get' extends Method<R> ? 'get' : Method<R>>(url: R, options: SilgiFetchOptions<R, M>) => Promise<FetchResponseData<R, M>>;
60
45
  declare function createSilgiFetch(options: FetchOptions | ((options: FetchOptions) => FetchOptions), localFetch?: typeof globalThis.$fetch): SilgiFetchClient;
61
46
 
62
47
  interface Options {
@@ -14,49 +14,34 @@ declare function silgiGenerateType(silgi: SilgiCLI): Promise<{
14
14
 
15
15
  declare function createSilgi(config: SilgiConfig): Promise<Silgi>;
16
16
 
17
- type StaticRoutes = keyof SilgiRouterTypes;
18
- type ExtractStaticPath<T extends string> = T extends `${infer Start}/:${string}` ? Start : T;
19
- type RouteWithParams<T extends string> = T | `${T}/${string}`;
20
- type ValidRoute = StaticRoutes | RouteWithParams<ExtractStaticPath<StaticRoutes & string>>;
21
- type FindMatchingRoute<T extends string> = {
22
- [K in StaticRoutes]: ExtractStaticPath<K & string> extends never ? never : T extends `${ExtractStaticPath<K & string>}${string}` ? K : never;
23
- }[StaticRoutes];
24
- type Method<R extends ValidRoute> = R extends StaticRoutes ? keyof SilgiRouterTypes[R] : keyof SilgiRouterTypes[FindMatchingRoute<R & string>];
25
- type DefaultMethodType = 'get' | 'post' | 'put' | 'delete';
26
- type FetchResponseData<R extends ValidRoute, M extends Method<R>> = R extends StaticRoutes ? SilgiRouterTypes[R][M & keyof SilgiRouterTypes[R]]['output'] : SilgiRouterTypes[FindMatchingRoute<R & string>][M & keyof SilgiRouterTypes[FindMatchingRoute<R & string>]]['output'];
27
- type RequestBodyOption<R extends ValidRoute, M extends Method<R>> = R extends StaticRoutes ? SilgiRouterTypes[R][M & keyof SilgiRouterTypes[R]] extends {
28
- input: infer I;
29
- } ? {
30
- body: I;
31
- } : {
32
- body?: never;
33
- } : SilgiRouterTypes[FindMatchingRoute<R & string>][M & keyof SilgiRouterTypes[FindMatchingRoute<R & string>]] extends {
34
- input: infer I;
35
- } ? {
36
- body: I;
37
- } : {
38
- body?: never;
39
- };
40
- type ExtractParams<T extends string> = T extends `${string}:${infer Param}/${infer Rest}` ? {
17
+ type TrimAfterFourSlashes<T extends string> = T extends `/${infer S1}/${infer S2}/${infer S3}/${infer _}` ? `/${S1}/${S2}/${S3}` : T;
18
+ type AllPaths = SilgiRouterTypes extends {
19
+ keys: infer U;
20
+ } ? keyof U extends never ? string : keyof U : string;
21
+ type ExtractPathParams<T extends string> = T extends `${infer _Start}:${infer Param}/${infer Rest}` ? {
41
22
  [K in Param]: string;
42
- } & ExtractParams<Rest> : T extends `${string}:${infer Param}` ? {
23
+ } & ExtractPathParams<Rest> : T extends `${infer _Start}:${infer Param}` ? {
43
24
  [K in Param]: string;
44
25
  } : object;
45
- type RouterParams<R extends ValidRoute, M extends Method<R>> = R extends StaticRoutes ? SilgiRouterTypes[R][M & keyof SilgiRouterTypes[R]] extends {
46
- params: infer P;
26
+ type ValidRoute = AllPaths | (string & {});
27
+ type Method<R extends ValidRoute> = TrimAfterFourSlashes<R> extends keyof SilgiRouterTypes ? keyof SilgiRouterTypes[TrimAfterFourSlashes<R>] : never;
28
+ type FetchResponseData<R extends ValidRoute, M extends Method<R>> = TrimAfterFourSlashes<R> extends keyof SilgiRouterTypes ? SilgiRouterTypes[TrimAfterFourSlashes<R>][M]['output'] : never;
29
+ type RequestBodyOption<R extends ValidRoute, M extends Method<R>> = TrimAfterFourSlashes<R> extends keyof SilgiRouterTypes ? SilgiRouterTypes[TrimAfterFourSlashes<R>][M] extends {
30
+ input: infer I;
47
31
  } ? {
48
- params: P;
49
- } : R extends `${string}:${string}` ? {
50
- params: ExtractParams<R>;
51
- } : {
52
- params?: never;
32
+ body: I;
53
33
  } : {
54
- params: ExtractParams<FindMatchingRoute<R & string>>;
55
- };
34
+ body?: never;
35
+ } : never;
36
+ type RouterParams<R extends ValidRoute> = ExtractPathParams<R>;
56
37
  type SilgiFetchOptions<R extends ValidRoute, M extends Method<R>> = {
57
38
  method: M;
58
- } & RequestBodyOption<R, M> & RouterParams<R, M> & Omit<FetchOptions, 'query' | 'body' | 'method'>;
59
- type SilgiFetchClient = <R extends ValidRoute, M extends Method<R> | DefaultMethodType = 'get'>(url: R, options: SilgiFetchOptions<R, M extends Method<R> ? M : Extract<Method<R>, DefaultMethodType>>) => Promise<FetchResponseData<R, M extends Method<R> ? M : Extract<Method<R>, DefaultMethodType>>>;
39
+ } & RequestBodyOption<R, M> & (RouterParams<R> extends object ? {
40
+ params: RouterParams<R>;
41
+ } : {
42
+ params?: never;
43
+ }) & Omit<FetchOptions, 'query' | 'body' | 'method'>;
44
+ type SilgiFetchClient = <R extends ValidRoute, M extends Method<R> = 'get' extends Method<R> ? 'get' : Method<R>>(url: R, options: SilgiFetchOptions<R, M>) => Promise<FetchResponseData<R, M>>;
60
45
  declare function createSilgiFetch(options: FetchOptions | ((options: FetchOptions) => FetchOptions), localFetch?: typeof globalThis.$fetch): SilgiFetchClient;
61
46
 
62
47
  interface Options {
@@ -1124,12 +1124,14 @@ async function createSilgi(config) {
1124
1124
  }
1125
1125
 
1126
1126
  function createSilgiFetch(options, localFetch) {
1127
- return (url, opts = {}) => {
1128
- opts = typeof options === "function" ? options(opts) : { ...options, ...opts };
1129
- if (!opts.method)
1130
- opts.method = "get";
1131
- const $fetch = getFetch(url, opts, localFetch);
1132
- return $fetch(fillPath(url, opts?.path), opts);
1127
+ return (url, opts) => {
1128
+ const finalOpts = {
1129
+ ...typeof options === "function" ? options(opts) : options,
1130
+ ...opts,
1131
+ method: opts.method || "get"
1132
+ };
1133
+ const $fetch = getFetch(url, finalOpts, localFetch);
1134
+ return $fetch(fillPath(url, opts.params), finalOpts);
1133
1135
  };
1134
1136
  }
1135
1137
  function getFetch(url, opts, localFetch) {
@@ -48,7 +48,7 @@ const module = {
48
48
  imports: ["silgi"]
49
49
  });
50
50
  nitro.options.imports.presets.push({
51
- from: "silgi/type",
51
+ from: "silgi/types",
52
52
  imports: autoImportTypes.map((type) => type),
53
53
  type: true
54
54
  });
@@ -20,7 +20,7 @@ const module = defineNuxtModule({
20
20
  nuxt.options.nitro.modules ||= [];
21
21
  nuxt.options.nitro.modules.push(await resolvePath("silgi/ecosystem/nitro"));
22
22
  nuxt.hook("prepare:types", ({ references }) => {
23
- references.push({ path: relativeWithDot(nuxt.options.buildDir, join(silgi.build.dir, "silgi.d.ts")) });
23
+ references.push({ path: relativeWithDot(nuxt.options.buildDir, join(silgi.build.typesDir, "silgi.d.ts")) });
24
24
  });
25
25
  nuxt.options.imports.presets.push({
26
26
  from: "silgi/types",
@@ -1,3 +1,3 @@
1
- const version = "0.4.8";
1
+ const version = "0.4.11";
2
2
 
3
3
  export { version };
@@ -1,3 +1,3 @@
1
- const version = "0.4.8";
1
+ const version = "0.4.11";
2
2
 
3
3
  export { version };
@@ -1,18 +1,18 @@
1
- import type { AvailableRouterMethod } from 'nitropack';
2
1
  import type { FetchError } from 'ofetch';
3
2
  import type { SilgiRouterTypes } from 'silgi/types';
4
- import { type AsyncData, type FetchResult, type UseFetchOptions } from 'nuxt/app';
5
- type RouteString<T extends keyof SilgiRouterTypes> = T | (string & {});
6
- export type KeysOf<T> = Array<T extends T ? keyof T extends string ? keyof T : never : never>;
7
- export type PickFrom<T, K extends Array<string>> = T extends Array<any> ? T : T extends Record<string, any> ? keyof T extends K[number] ? T : K[number] extends never ? T : Pick<T, K[number]> : T;
8
- export declare function useSilgiFetch<ResT = void, ErrorT = FetchError, ReqT extends keyof SilgiRouterTypes = keyof SilgiRouterTypes, Method extends AvailableRouterMethod<ReqT> = ResT extends void ? 'get' extends AvailableRouterMethod<ReqT> ? 'get' : AvailableRouterMethod<ReqT> : AvailableRouterMethod<ReqT>, _ResT = ResT extends void ? FetchResult<ReqT, Method> : ResT, DataT = SilgiRouterTypes[ReqT][Method] extends {
9
- output: any;
10
- } ? SilgiRouterTypes[ReqT][Method]['output'] : any, PickKeys extends KeysOf<DataT> = KeysOf<DataT>, DefaultT = SilgiRouterTypes[ReqT][Method] extends {
11
- output: infer T;
12
- } ? T : never>(url: RouteString<ReqT> | (() => RouteString<ReqT>), options?: Omit<UseFetchOptions<any, any, any, any, ReqT>, 'method' | 'body'> & {
13
- method?: keyof SilgiRouterTypes[ReqT];
14
- body?: SilgiRouterTypes[ReqT][Method] extends {
15
- input: any;
16
- } ? SilgiRouterTypes[ReqT][Method]['input'] : never;
17
- }): AsyncData<DefaultT | PickFrom<DataT, PickKeys>, ErrorT | null>;
3
+ import { type AsyncData } from 'nuxt/app';
4
+ type TrimAfterFourSlashes<T extends string> = T extends `/${infer S1}/${infer S2}/${infer S3}/${infer _}` ? `/${S1}/${S2}/${S3}` : T;
5
+ type AllPaths = SilgiRouterTypes extends {
6
+ keys: infer U;
7
+ } ? keyof U extends never ? string : keyof U : string;
8
+ type ExtractPathParams<T extends string> = T extends `${infer _Start}:${infer Param}/${infer Rest}` ? {
9
+ [K in Param]: string;
10
+ } & ExtractPathParams<Rest> : T extends `${infer _Start}:${infer Param}` ? {
11
+ [K in Param]: string;
12
+ } : unknown;
13
+ export declare function useSilgiFetch<P extends AllPaths | (string & {}), BasePath extends keyof SilgiRouterTypes = TrimAfterFourSlashes<P> extends keyof SilgiRouterTypes ? TrimAfterFourSlashes<P> : never, M extends keyof SilgiRouterTypes[BasePath] = keyof SilgiRouterTypes[BasePath]>(path: P, options?: {
14
+ method?: M;
15
+ params?: ExtractPathParams<P>;
16
+ body?: SilgiRouterTypes[BasePath][M]['input'];
17
+ }): AsyncData<SilgiRouterTypes[BasePath][M]["output"], FetchError | null>;
18
18
  export {};
@@ -1,8 +1,11 @@
1
1
  import { useFetch } from "nuxt/app";
2
- export function useSilgiFetch(url, options) {
3
- const resolvedUrl = typeof url === "function" ? url() : url;
4
- return useFetch(resolvedUrl, {
5
- ...options,
6
- method: options?.method?.toUpperCase() ?? "GET"
2
+ function replacePathParams(template, params) {
3
+ return template.replace(/:([^/]+)/g, (_, param) => params[param] || `:${param}`);
4
+ }
5
+ export function useSilgiFetch(path, options) {
6
+ const resolvedPath = options?.params ? replacePathParams(path, options.params) : path;
7
+ return useFetch(resolvedPath, {
8
+ method: options?.method,
9
+ body: options?.body
7
10
  });
8
11
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "silgi",
3
3
  "type": "module",
4
- "version": "0.4.8",
4
+ "version": "0.4.11",
5
5
  "sideEffects": false,
6
6
  "exports": {
7
7
  "./cli": {