silgi 0.0.1

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/README.md ADDED
@@ -0,0 +1,2 @@
1
+ ```ts
2
+ ``
@@ -0,0 +1,37 @@
1
+ import { D as DefaultContext, a as DefaultInterface, E as ExtractScope, S as ServicePluginConfig, b as ServiceMethod, c as ExtractShared, C as CacheScope, d as CacheKeyGenerator, M as MethodPluginConfig, e as ServiceExecutor, O as OmitSilgiOptions, I as InitSilgi, P as PergelPlugin, f as Silgi } from './shared/silgi.BmG_a4Ft.mjs';
2
+ export { h as DefaultHooks, g as InitializedCreateScope, k as SchemaGenerated, i as SilgiDefaultContext, j as SilgiDefaultInterface } from './shared/silgi.BmG_a4Ft.mjs';
3
+ import * as unctx from 'unctx';
4
+ import 'consola';
5
+ import 'h3';
6
+ import 'zod';
7
+ import 'hookable';
8
+
9
+ declare function defineService<TContext extends DefaultContext = DefaultContext, TInterface extends DefaultInterface = DefaultInterface>(): {
10
+ scope<TScope extends keyof ExtractScope<TInterface>>(scope: TScope): {
11
+ service<TService extends keyof ExtractScope<TInterface>[TScope]>(name: TService, config?: ServicePluginConfig<TInterface> | undefined): {
12
+ methods<TMethods extends { [K in keyof ExtractScope<TInterface>[TScope][TService]]: {
13
+ handler: (input: ExtractScope<TInterface>[TScope][TService][K] extends ServiceMethod<infer I, any, DefaultInterface> ? I : never, shared: ExtractShared<TInterface>, context: TContext) => Promise<ExtractScope<TInterface>[TScope][TService][K] extends ServiceMethod<any, infer O, DefaultInterface> ? O : never>;
14
+ options?: {
15
+ cache?: {
16
+ ttl: number;
17
+ scope?: CacheScope;
18
+ key?: CacheKeyGenerator<any>;
19
+ namespace?: string;
20
+ } | undefined;
21
+ plugins?: MethodPluginConfig<TInterface> | undefined;
22
+ } | undefined;
23
+ }; }>(methodsConfig: TMethods): ServiceExecutor<ExtractScope<TInterface>[TScope][TService], TContext, TInterface>;
24
+ };
25
+ };
26
+ };
27
+ declare function createSilgi<TContext extends DefaultContext, TInterface extends DefaultInterface>(options: OmitSilgiOptions<TContext, TInterface>): InitSilgi<TContext, TInterface>;
28
+ declare function defineSilgiPlugin<TPluginConfig, TContext extends DefaultContext = DefaultContext, TInterface extends DefaultInterface = DefaultInterface>(plugin: Omit<PergelPlugin<TContext, TInterface, TPluginConfig>, 'defaults'> & {
29
+ defaults?: TPluginConfig;
30
+ }): (options?: TPluginConfig) => PergelPlugin<TContext, TInterface, TPluginConfig>;
31
+
32
+ declare const silgiCtx: unctx.UseContext<InitSilgi<DefaultContext, DefaultInterface>>;
33
+ declare function useShared<TContext extends DefaultContext, TInterface extends DefaultInterface>(): Silgi<TContext, TInterface>["options"]["shared"];
34
+ declare function useSilgi(): InitSilgi<DefaultContext, DefaultInterface>;
35
+ declare function useHook<TContext extends DefaultContext, TInterface extends DefaultInterface>(): Silgi<TContext, TInterface>["hooks"]["hook"];
36
+
37
+ export { DefaultContext, DefaultInterface, InitSilgi, OmitSilgiOptions, createSilgi, defineService, defineSilgiPlugin, silgiCtx, useHook, useShared, useSilgi };
@@ -0,0 +1,37 @@
1
+ import { D as DefaultContext, a as DefaultInterface, E as ExtractScope, S as ServicePluginConfig, b as ServiceMethod, c as ExtractShared, C as CacheScope, d as CacheKeyGenerator, M as MethodPluginConfig, e as ServiceExecutor, O as OmitSilgiOptions, I as InitSilgi, P as PergelPlugin, f as Silgi } from './shared/silgi.BmG_a4Ft.js';
2
+ export { h as DefaultHooks, g as InitializedCreateScope, k as SchemaGenerated, i as SilgiDefaultContext, j as SilgiDefaultInterface } from './shared/silgi.BmG_a4Ft.js';
3
+ import * as unctx from 'unctx';
4
+ import 'consola';
5
+ import 'h3';
6
+ import 'zod';
7
+ import 'hookable';
8
+
9
+ declare function defineService<TContext extends DefaultContext = DefaultContext, TInterface extends DefaultInterface = DefaultInterface>(): {
10
+ scope<TScope extends keyof ExtractScope<TInterface>>(scope: TScope): {
11
+ service<TService extends keyof ExtractScope<TInterface>[TScope]>(name: TService, config?: ServicePluginConfig<TInterface> | undefined): {
12
+ methods<TMethods extends { [K in keyof ExtractScope<TInterface>[TScope][TService]]: {
13
+ handler: (input: ExtractScope<TInterface>[TScope][TService][K] extends ServiceMethod<infer I, any, DefaultInterface> ? I : never, shared: ExtractShared<TInterface>, context: TContext) => Promise<ExtractScope<TInterface>[TScope][TService][K] extends ServiceMethod<any, infer O, DefaultInterface> ? O : never>;
14
+ options?: {
15
+ cache?: {
16
+ ttl: number;
17
+ scope?: CacheScope;
18
+ key?: CacheKeyGenerator<any>;
19
+ namespace?: string;
20
+ } | undefined;
21
+ plugins?: MethodPluginConfig<TInterface> | undefined;
22
+ } | undefined;
23
+ }; }>(methodsConfig: TMethods): ServiceExecutor<ExtractScope<TInterface>[TScope][TService], TContext, TInterface>;
24
+ };
25
+ };
26
+ };
27
+ declare function createSilgi<TContext extends DefaultContext, TInterface extends DefaultInterface>(options: OmitSilgiOptions<TContext, TInterface>): InitSilgi<TContext, TInterface>;
28
+ declare function defineSilgiPlugin<TPluginConfig, TContext extends DefaultContext = DefaultContext, TInterface extends DefaultInterface = DefaultInterface>(plugin: Omit<PergelPlugin<TContext, TInterface, TPluginConfig>, 'defaults'> & {
29
+ defaults?: TPluginConfig;
30
+ }): (options?: TPluginConfig) => PergelPlugin<TContext, TInterface, TPluginConfig>;
31
+
32
+ declare const silgiCtx: unctx.UseContext<InitSilgi<DefaultContext, DefaultInterface>>;
33
+ declare function useShared<TContext extends DefaultContext, TInterface extends DefaultInterface>(): Silgi<TContext, TInterface>["options"]["shared"];
34
+ declare function useSilgi(): InitSilgi<DefaultContext, DefaultInterface>;
35
+ declare function useHook<TContext extends DefaultContext, TInterface extends DefaultInterface>(): Silgi<TContext, TInterface>["hooks"]["hook"];
36
+
37
+ export { DefaultContext, DefaultInterface, InitSilgi, OmitSilgiOptions, createSilgi, defineService, defineSilgiPlugin, silgiCtx, useHook, useShared, useSilgi };
package/dist/index.mjs ADDED
@@ -0,0 +1,7 @@
1
+ export { c as createSilgi, d as defineService, a as defineSilgiPlugin, s as silgiCtx, e as useHook, u as useShared, b as useSilgi } from './shared/silgi.9QqTROsY.mjs';
2
+ import 'consola';
3
+ import 'hookable';
4
+ import 'node:buffer';
5
+ import 'node:fs';
6
+ import 'node:path';
7
+ import 'unctx';
@@ -0,0 +1,138 @@
1
+ import * as openapi3_ts_oas31 from 'openapi3-ts/oas31';
2
+ import { SchemaObject, ServerObject, OpenAPIObject } from 'openapi3-ts/oas31';
3
+ import { P as PergelPlugin, D as DefaultContext, a as DefaultInterface, I as InitSilgi } from '../shared/silgi.BmG_a4Ft.mjs';
4
+ import { z } from 'zod';
5
+ import 'consola';
6
+ import 'h3';
7
+ import 'hookable';
8
+
9
+ interface DefaultSilgiPlugin {
10
+ openapi?: {
11
+ schema?: {
12
+ input?: SchemaObject;
13
+ output?: SchemaObject;
14
+ };
15
+ };
16
+ }
17
+ declare module 'silgi' {
18
+ interface DefaultInterface {
19
+ plugins: DefaultSilgiPlugin;
20
+ }
21
+ }
22
+ interface OpenAPIPluginConfig {
23
+ title?: string;
24
+ version?: string;
25
+ description?: string;
26
+ servers?: ServerObject[];
27
+ writeToFile?: boolean;
28
+ outputPath?: string;
29
+ }
30
+ declare const createOpenAPIPlugin: (options?: OpenAPIPluginConfig | undefined) => PergelPlugin<DefaultContext, DefaultInterface, OpenAPIPluginConfig>;
31
+ declare function createSchema(schema: {
32
+ input?: Partial<SchemaObject>;
33
+ output?: Partial<SchemaObject>;
34
+ }): {
35
+ input: {
36
+ discriminator?: openapi3_ts_oas31.DiscriminatorObject;
37
+ readOnly?: boolean;
38
+ writeOnly?: boolean;
39
+ xml?: openapi3_ts_oas31.XmlObject;
40
+ externalDocs?: openapi3_ts_oas31.ExternalDocumentationObject;
41
+ example?: any;
42
+ examples?: any[];
43
+ deprecated?: boolean;
44
+ type: string | openapi3_ts_oas31.SchemaObjectType[];
45
+ format?: "int32" | "int64" | "float" | "double" | "byte" | "binary" | "date" | "date-time" | "password" | string;
46
+ allOf?: (SchemaObject | openapi3_ts_oas31.ReferenceObject)[];
47
+ oneOf?: (SchemaObject | openapi3_ts_oas31.ReferenceObject)[];
48
+ anyOf?: (SchemaObject | openapi3_ts_oas31.ReferenceObject)[];
49
+ not?: SchemaObject | openapi3_ts_oas31.ReferenceObject;
50
+ items?: SchemaObject | openapi3_ts_oas31.ReferenceObject;
51
+ properties?: {
52
+ [propertyName: string]: SchemaObject | openapi3_ts_oas31.ReferenceObject;
53
+ };
54
+ additionalProperties?: SchemaObject | openapi3_ts_oas31.ReferenceObject | boolean;
55
+ propertyNames?: SchemaObject | openapi3_ts_oas31.ReferenceObject;
56
+ description?: string;
57
+ default?: any;
58
+ title?: string;
59
+ multipleOf?: number;
60
+ maximum?: number;
61
+ const?: any;
62
+ exclusiveMaximum?: number;
63
+ minimum?: number;
64
+ exclusiveMinimum?: number;
65
+ maxLength?: number;
66
+ minLength?: number;
67
+ pattern?: string;
68
+ maxItems?: number;
69
+ minItems?: number;
70
+ uniqueItems?: boolean;
71
+ maxProperties?: number;
72
+ minProperties?: number;
73
+ required?: string[];
74
+ enum?: any[];
75
+ prefixItems?: (SchemaObject | openapi3_ts_oas31.ReferenceObject)[];
76
+ contentMediaType?: string;
77
+ contentEncoding?: string;
78
+ };
79
+ output: {
80
+ discriminator?: openapi3_ts_oas31.DiscriminatorObject;
81
+ readOnly?: boolean;
82
+ writeOnly?: boolean;
83
+ xml?: openapi3_ts_oas31.XmlObject;
84
+ externalDocs?: openapi3_ts_oas31.ExternalDocumentationObject;
85
+ example?: any;
86
+ examples?: any[];
87
+ deprecated?: boolean;
88
+ type: string | openapi3_ts_oas31.SchemaObjectType[];
89
+ format?: "int32" | "int64" | "float" | "double" | "byte" | "binary" | "date" | "date-time" | "password" | string;
90
+ allOf?: (SchemaObject | openapi3_ts_oas31.ReferenceObject)[];
91
+ oneOf?: (SchemaObject | openapi3_ts_oas31.ReferenceObject)[];
92
+ anyOf?: (SchemaObject | openapi3_ts_oas31.ReferenceObject)[];
93
+ not?: SchemaObject | openapi3_ts_oas31.ReferenceObject;
94
+ items?: SchemaObject | openapi3_ts_oas31.ReferenceObject;
95
+ properties?: {
96
+ [propertyName: string]: SchemaObject | openapi3_ts_oas31.ReferenceObject;
97
+ };
98
+ additionalProperties?: SchemaObject | openapi3_ts_oas31.ReferenceObject | boolean;
99
+ propertyNames?: SchemaObject | openapi3_ts_oas31.ReferenceObject;
100
+ description?: string;
101
+ default?: any;
102
+ title?: string;
103
+ multipleOf?: number;
104
+ maximum?: number;
105
+ const?: any;
106
+ exclusiveMaximum?: number;
107
+ minimum?: number;
108
+ exclusiveMinimum?: number;
109
+ maxLength?: number;
110
+ minLength?: number;
111
+ pattern?: string;
112
+ maxItems?: number;
113
+ minItems?: number;
114
+ uniqueItems?: boolean;
115
+ maxProperties?: number;
116
+ minProperties?: number;
117
+ required?: string[];
118
+ enum?: any[];
119
+ prefixItems?: (SchemaObject | openapi3_ts_oas31.ReferenceObject)[];
120
+ contentMediaType?: string;
121
+ contentEncoding?: string;
122
+ };
123
+ };
124
+ declare function createSchemaFromZod(schema: {
125
+ input?: z.ZodType<any>;
126
+ output?: z.ZodType<any>;
127
+ }): {
128
+ input: SchemaObject | {
129
+ type: string;
130
+ };
131
+ output: SchemaObject | {
132
+ type: string;
133
+ };
134
+ };
135
+ declare function getOpenAPIDocument(initSilgi: InitSilgi<any, any>): OpenAPIObject;
136
+ declare function getOpenAPIPath(initSilgi: InitSilgi<any, any>): string | undefined;
137
+
138
+ export { type DefaultSilgiPlugin, type OpenAPIPluginConfig, createOpenAPIPlugin, createSchema, createSchemaFromZod, getOpenAPIDocument, getOpenAPIPath };
@@ -0,0 +1,138 @@
1
+ import * as openapi3_ts_oas31 from 'openapi3-ts/oas31';
2
+ import { SchemaObject, ServerObject, OpenAPIObject } from 'openapi3-ts/oas31';
3
+ import { P as PergelPlugin, D as DefaultContext, a as DefaultInterface, I as InitSilgi } from '../shared/silgi.BmG_a4Ft.js';
4
+ import { z } from 'zod';
5
+ import 'consola';
6
+ import 'h3';
7
+ import 'hookable';
8
+
9
+ interface DefaultSilgiPlugin {
10
+ openapi?: {
11
+ schema?: {
12
+ input?: SchemaObject;
13
+ output?: SchemaObject;
14
+ };
15
+ };
16
+ }
17
+ declare module 'silgi' {
18
+ interface DefaultInterface {
19
+ plugins: DefaultSilgiPlugin;
20
+ }
21
+ }
22
+ interface OpenAPIPluginConfig {
23
+ title?: string;
24
+ version?: string;
25
+ description?: string;
26
+ servers?: ServerObject[];
27
+ writeToFile?: boolean;
28
+ outputPath?: string;
29
+ }
30
+ declare const createOpenAPIPlugin: (options?: OpenAPIPluginConfig | undefined) => PergelPlugin<DefaultContext, DefaultInterface, OpenAPIPluginConfig>;
31
+ declare function createSchema(schema: {
32
+ input?: Partial<SchemaObject>;
33
+ output?: Partial<SchemaObject>;
34
+ }): {
35
+ input: {
36
+ discriminator?: openapi3_ts_oas31.DiscriminatorObject;
37
+ readOnly?: boolean;
38
+ writeOnly?: boolean;
39
+ xml?: openapi3_ts_oas31.XmlObject;
40
+ externalDocs?: openapi3_ts_oas31.ExternalDocumentationObject;
41
+ example?: any;
42
+ examples?: any[];
43
+ deprecated?: boolean;
44
+ type: string | openapi3_ts_oas31.SchemaObjectType[];
45
+ format?: "int32" | "int64" | "float" | "double" | "byte" | "binary" | "date" | "date-time" | "password" | string;
46
+ allOf?: (SchemaObject | openapi3_ts_oas31.ReferenceObject)[];
47
+ oneOf?: (SchemaObject | openapi3_ts_oas31.ReferenceObject)[];
48
+ anyOf?: (SchemaObject | openapi3_ts_oas31.ReferenceObject)[];
49
+ not?: SchemaObject | openapi3_ts_oas31.ReferenceObject;
50
+ items?: SchemaObject | openapi3_ts_oas31.ReferenceObject;
51
+ properties?: {
52
+ [propertyName: string]: SchemaObject | openapi3_ts_oas31.ReferenceObject;
53
+ };
54
+ additionalProperties?: SchemaObject | openapi3_ts_oas31.ReferenceObject | boolean;
55
+ propertyNames?: SchemaObject | openapi3_ts_oas31.ReferenceObject;
56
+ description?: string;
57
+ default?: any;
58
+ title?: string;
59
+ multipleOf?: number;
60
+ maximum?: number;
61
+ const?: any;
62
+ exclusiveMaximum?: number;
63
+ minimum?: number;
64
+ exclusiveMinimum?: number;
65
+ maxLength?: number;
66
+ minLength?: number;
67
+ pattern?: string;
68
+ maxItems?: number;
69
+ minItems?: number;
70
+ uniqueItems?: boolean;
71
+ maxProperties?: number;
72
+ minProperties?: number;
73
+ required?: string[];
74
+ enum?: any[];
75
+ prefixItems?: (SchemaObject | openapi3_ts_oas31.ReferenceObject)[];
76
+ contentMediaType?: string;
77
+ contentEncoding?: string;
78
+ };
79
+ output: {
80
+ discriminator?: openapi3_ts_oas31.DiscriminatorObject;
81
+ readOnly?: boolean;
82
+ writeOnly?: boolean;
83
+ xml?: openapi3_ts_oas31.XmlObject;
84
+ externalDocs?: openapi3_ts_oas31.ExternalDocumentationObject;
85
+ example?: any;
86
+ examples?: any[];
87
+ deprecated?: boolean;
88
+ type: string | openapi3_ts_oas31.SchemaObjectType[];
89
+ format?: "int32" | "int64" | "float" | "double" | "byte" | "binary" | "date" | "date-time" | "password" | string;
90
+ allOf?: (SchemaObject | openapi3_ts_oas31.ReferenceObject)[];
91
+ oneOf?: (SchemaObject | openapi3_ts_oas31.ReferenceObject)[];
92
+ anyOf?: (SchemaObject | openapi3_ts_oas31.ReferenceObject)[];
93
+ not?: SchemaObject | openapi3_ts_oas31.ReferenceObject;
94
+ items?: SchemaObject | openapi3_ts_oas31.ReferenceObject;
95
+ properties?: {
96
+ [propertyName: string]: SchemaObject | openapi3_ts_oas31.ReferenceObject;
97
+ };
98
+ additionalProperties?: SchemaObject | openapi3_ts_oas31.ReferenceObject | boolean;
99
+ propertyNames?: SchemaObject | openapi3_ts_oas31.ReferenceObject;
100
+ description?: string;
101
+ default?: any;
102
+ title?: string;
103
+ multipleOf?: number;
104
+ maximum?: number;
105
+ const?: any;
106
+ exclusiveMaximum?: number;
107
+ minimum?: number;
108
+ exclusiveMinimum?: number;
109
+ maxLength?: number;
110
+ minLength?: number;
111
+ pattern?: string;
112
+ maxItems?: number;
113
+ minItems?: number;
114
+ uniqueItems?: boolean;
115
+ maxProperties?: number;
116
+ minProperties?: number;
117
+ required?: string[];
118
+ enum?: any[];
119
+ prefixItems?: (SchemaObject | openapi3_ts_oas31.ReferenceObject)[];
120
+ contentMediaType?: string;
121
+ contentEncoding?: string;
122
+ };
123
+ };
124
+ declare function createSchemaFromZod(schema: {
125
+ input?: z.ZodType<any>;
126
+ output?: z.ZodType<any>;
127
+ }): {
128
+ input: SchemaObject | {
129
+ type: string;
130
+ };
131
+ output: SchemaObject | {
132
+ type: string;
133
+ };
134
+ };
135
+ declare function getOpenAPIDocument(initSilgi: InitSilgi<any, any>): OpenAPIObject;
136
+ declare function getOpenAPIPath(initSilgi: InitSilgi<any, any>): string | undefined;
137
+
138
+ export { type DefaultSilgiPlugin, type OpenAPIPluginConfig, createOpenAPIPlugin, createSchema, createSchemaFromZod, getOpenAPIDocument, getOpenAPIPath };
@@ -0,0 +1,204 @@
1
+ import { mkdir, writeFile } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ import { generateSchema } from '@anatine/zod-openapi';
4
+ import consola from 'consola';
5
+ import { a as defineSilgiPlugin } from '../shared/silgi.9QqTROsY.mjs';
6
+ import 'hookable';
7
+ import 'node:buffer';
8
+ import 'node:fs';
9
+ import 'unctx';
10
+
11
+ function parseMethodName(methodName) {
12
+ const [method, ...pathParts] = methodName.split(":");
13
+ const path = pathParts.join(":");
14
+ return !path || !isHttpMethod(method) ? { method: "post", path: methodName } : { method, path };
15
+ }
16
+ function isHttpMethod(method) {
17
+ return ["get", "post", "put", "delete", "patch"].includes(method.toLowerCase());
18
+ }
19
+ const createOpenAPIPlugin = defineSilgiPlugin({
20
+ name: "openapi",
21
+ async setup(config, initSilgi) {
22
+ const document = {
23
+ openapi: "3.1",
24
+ info: {
25
+ title: config.title || "API Documentation",
26
+ version: config.version || "1.0.0",
27
+ description: config.description || ""
28
+ },
29
+ servers: config.servers || [{
30
+ url: "/",
31
+ description: "Default Server",
32
+ // Required by ServerObject
33
+ variables: {}
34
+ // Required by ServerObject
35
+ }],
36
+ paths: {},
37
+ components: {
38
+ schemas: {
39
+ Success: {
40
+ description: "Success response",
41
+ type: "object",
42
+ required: ["success"],
43
+ properties: {
44
+ success: { type: "boolean", enum: [true] },
45
+ data: { type: "object" }
46
+ }
47
+ },
48
+ Error: {
49
+ description: "Error response",
50
+ type: "object",
51
+ required: ["success", "error"],
52
+ properties: {
53
+ success: { type: "boolean", enum: [false] },
54
+ error: {
55
+ type: "object",
56
+ properties: {
57
+ code: { type: "string" },
58
+ message: { type: "string" }
59
+ }
60
+ }
61
+ }
62
+ }
63
+ }
64
+ }
65
+ };
66
+ const paths = {};
67
+ const services = initSilgi.getAllServices();
68
+ for (const [scopeName, scope] of Object.entries(services)) {
69
+ for (const serviceName of scope.services) {
70
+ const service = initSilgi.getService(scopeName, serviceName);
71
+ for (const [methodName, handler] of service.handlers.entries()) {
72
+ let schema = handler.options?.plugins?.openapi?.schema;
73
+ const sharedZod = initSilgi.silgi.options.shared[scopeName]?.zod;
74
+ if (!schema && sharedZod) {
75
+ try {
76
+ schema = createSchemaFromZod(sharedZod[serviceName][methodName]);
77
+ } catch {
78
+ if (!schema?.input || !schema?.output) {
79
+ consola.withTag("silgi:openapi").info("Please add schema for", scopeName, serviceName, methodName);
80
+ }
81
+ }
82
+ }
83
+ const { method, path } = parseMethodName(String(methodName));
84
+ const route = `/${scopeName}/${serviceName}/${path}`;
85
+ const operation = {
86
+ tags: [scopeName, serviceName],
87
+ summary: String(methodName).split(":")[1] || String(methodName),
88
+ description: String(`Handle copy: defineService().scope('${scopeName}').service('${serviceName}').methods({ '${String(methodName)}': { ... } })`),
89
+ parameters: (() => {
90
+ switch (method) {
91
+ case "get":
92
+ if (!schema?.input)
93
+ return [];
94
+ return Object.entries(schema.input.properties || {}).map(([paramName, paramSchema]) => ({
95
+ in: "query",
96
+ name: paramName,
97
+ required: schema.input?.required?.includes(paramName) || false,
98
+ schema: paramSchema,
99
+ description: paramSchema.description || void 0,
100
+ $ref: paramSchema.$ref || void 0
101
+ }));
102
+ case "delete":
103
+ return [{
104
+ in: "path",
105
+ name: "id",
106
+ required: true,
107
+ schema: { type: "string" }
108
+ }];
109
+ case "put":
110
+ case "patch":
111
+ return [{
112
+ in: "path",
113
+ name: "id",
114
+ required: true,
115
+ schema: { type: "string" }
116
+ }];
117
+ default:
118
+ return [];
119
+ }
120
+ })(),
121
+ ...method !== "get" && method !== "delete" ? {
122
+ requestBody: {
123
+ required: true,
124
+ content: {
125
+ "application/json": {
126
+ schema: schema?.input || { type: "object" }
127
+ }
128
+ }
129
+ }
130
+ } : {},
131
+ responses: {
132
+ 200: {
133
+ description: "Successful operation",
134
+ content: {
135
+ "application/json": {
136
+ schema: {
137
+ type: "object",
138
+ required: ["success"],
139
+ properties: {
140
+ success: { type: "boolean", enum: [true] },
141
+ data: schema?.output || { type: "object" }
142
+ }
143
+ }
144
+ }
145
+ }
146
+ },
147
+ 400: {
148
+ description: "Operation failed",
149
+ content: {
150
+ "application/json": {
151
+ schema: { $ref: "#/components/schemas/Error" }
152
+ }
153
+ }
154
+ }
155
+ }
156
+ };
157
+ paths[route] = {
158
+ ...paths[route],
159
+ [method]: operation
160
+ };
161
+ }
162
+ }
163
+ }
164
+ document.paths = paths;
165
+ initSilgi.setEvent("openapi", document);
166
+ if (config.writeToFile) {
167
+ try {
168
+ const silgiDir = join(process.cwd(), ".silgi");
169
+ await mkdir(silgiDir, { recursive: true });
170
+ const outputJSON = join(silgiDir, "openapi.json");
171
+ await writeFile(outputJSON, JSON.stringify(document, null, 2), "utf-8");
172
+ } catch (error) {
173
+ initSilgi.silgi.logger.error("Failed to generate OpenAPI documentation:", error);
174
+ throw error;
175
+ }
176
+ }
177
+ }
178
+ });
179
+ function createSchema(schema) {
180
+ return {
181
+ input: {
182
+ type: "object",
183
+ ...schema.input
184
+ },
185
+ output: {
186
+ type: "object",
187
+ ...schema.output
188
+ }
189
+ };
190
+ }
191
+ function createSchemaFromZod(schema) {
192
+ return {
193
+ input: schema.input ? generateSchema(schema.input) : { type: "object" },
194
+ output: schema.output ? generateSchema(schema.output) : { type: "object" }
195
+ };
196
+ }
197
+ function getOpenAPIDocument(initSilgi) {
198
+ return initSilgi.getEvent("openapi:document");
199
+ }
200
+ function getOpenAPIPath(initSilgi) {
201
+ return initSilgi.getEvent("openapi:path");
202
+ }
203
+
204
+ export { createOpenAPIPlugin, createSchema, createSchemaFromZod, getOpenAPIDocument, getOpenAPIPath };
@@ -0,0 +1,14 @@
1
+ import { P as PergelPlugin, D as DefaultContext, a as DefaultInterface } from '../shared/silgi.BmG_a4Ft.mjs';
2
+ import 'consola';
3
+ import 'h3';
4
+ import 'zod';
5
+ import 'hookable';
6
+
7
+ interface SilgiPluginConfig {
8
+ writeToFile?: boolean;
9
+ theme?: 'alternate' | 'default' | 'moon' | 'purple' | 'solarized' | 'bluePlanet' | 'saturn' | 'kepler' | 'mars' | 'deepSpace' | 'none';
10
+ outputPath?: string;
11
+ }
12
+ declare const createScalarPlugin: (options?: SilgiPluginConfig | undefined) => PergelPlugin<DefaultContext, DefaultInterface, SilgiPluginConfig>;
13
+
14
+ export { type SilgiPluginConfig, createScalarPlugin };
@@ -0,0 +1,14 @@
1
+ import { P as PergelPlugin, D as DefaultContext, a as DefaultInterface } from '../shared/silgi.BmG_a4Ft.js';
2
+ import 'consola';
3
+ import 'h3';
4
+ import 'zod';
5
+ import 'hookable';
6
+
7
+ interface SilgiPluginConfig {
8
+ writeToFile?: boolean;
9
+ theme?: 'alternate' | 'default' | 'moon' | 'purple' | 'solarized' | 'bluePlanet' | 'saturn' | 'kepler' | 'mars' | 'deepSpace' | 'none';
10
+ outputPath?: string;
11
+ }
12
+ declare const createScalarPlugin: (options?: SilgiPluginConfig | undefined) => PergelPlugin<DefaultContext, DefaultInterface, SilgiPluginConfig>;
13
+
14
+ export { type SilgiPluginConfig, createScalarPlugin };
@@ -0,0 +1,66 @@
1
+ import { mkdir, writeFile } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ import { defineEventHandler } from 'h3';
4
+ import { a as defineSilgiPlugin } from '../shared/silgi.9QqTROsY.mjs';
5
+ import 'consola';
6
+ import 'hookable';
7
+ import 'node:buffer';
8
+ import 'node:fs';
9
+ import 'unctx';
10
+
11
+ function generateHTML(openapi, config) {
12
+ return `<!doctype html>
13
+ <html>
14
+ <head>
15
+ <title>${openapi.info.title || "API Documentation"}</title>
16
+ <meta charset="utf-8" />
17
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
18
+ </head>
19
+ <body>
20
+ <script id="api-reference" type="application/json">
21
+ ${JSON.stringify(openapi, null, 2)}
22
+ <\/script>
23
+
24
+ <script>
25
+ var configuration = {
26
+ theme: '${config.theme || "deepSpace"}'
27
+ }
28
+ document.getElementById('api-reference').dataset.configuration = JSON.stringify(configuration)
29
+ <\/script>
30
+
31
+ <script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"><\/script>
32
+ </body>
33
+ </html>`;
34
+ }
35
+ const createScalarPlugin = defineSilgiPlugin({
36
+ name: "scalar",
37
+ dependencies: ["openapi"],
38
+ async setup(config, initSilgi) {
39
+ const openapi = initSilgi.getEvent("openapi");
40
+ if (!openapi)
41
+ throw new Error("OpenAPI document not found. Make sure openapi plugin is initialized before scalar");
42
+ const scalarHandler = defineEventHandler(async (_event) => {
43
+ return generateHTML(openapi, config);
44
+ });
45
+ const routePath = "/api/_silgi/scalar";
46
+ initSilgi.silgi.hook("h3:app:setup", (router) => {
47
+ router.use(routePath, scalarHandler);
48
+ });
49
+ if (!config.writeToFile)
50
+ return;
51
+ try {
52
+ const silgiDir = join(process.cwd(), ".silgi");
53
+ await mkdir(silgiDir, { recursive: true });
54
+ const outputPath = config.outputPath || "docs";
55
+ const docsDir = join(silgiDir, outputPath);
56
+ await mkdir(docsDir, { recursive: true });
57
+ const htmlPath = join(docsDir, "index.html");
58
+ await writeFile(htmlPath, generateHTML(openapi, config), "utf-8");
59
+ } catch (error) {
60
+ initSilgi.silgi.logger.error("Failed to generate Scalar documentation:", error);
61
+ throw error;
62
+ }
63
+ }
64
+ });
65
+
66
+ export { createScalarPlugin };