@hypequery/serve 0.0.7 → 0.0.9

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.
Files changed (76) hide show
  1. package/dist/adapters/fetch.d.ts +3 -0
  2. package/dist/adapters/fetch.d.ts.map +1 -0
  3. package/dist/adapters/fetch.js +26 -0
  4. package/dist/adapters/node.d.ts +8 -0
  5. package/dist/adapters/node.d.ts.map +1 -0
  6. package/dist/adapters/node.js +105 -0
  7. package/dist/adapters/utils.d.ts +39 -0
  8. package/dist/adapters/utils.d.ts.map +1 -0
  9. package/dist/adapters/utils.js +114 -0
  10. package/dist/adapters/vercel.d.ts +7 -0
  11. package/dist/adapters/vercel.d.ts.map +1 -0
  12. package/dist/adapters/vercel.js +13 -0
  13. package/dist/auth.d.ts +192 -0
  14. package/dist/auth.d.ts.map +1 -0
  15. package/dist/auth.js +221 -0
  16. package/dist/builder.d.ts +3 -0
  17. package/dist/builder.d.ts.map +1 -0
  18. package/dist/builder.js +56 -0
  19. package/dist/client-config.d.ts +44 -0
  20. package/dist/client-config.d.ts.map +1 -0
  21. package/dist/client-config.js +53 -0
  22. package/dist/dev.d.ts +9 -0
  23. package/dist/dev.d.ts.map +1 -0
  24. package/dist/dev.js +30 -0
  25. package/dist/docs-ui.d.ts +3 -0
  26. package/dist/docs-ui.d.ts.map +1 -0
  27. package/dist/docs-ui.js +34 -0
  28. package/dist/endpoint.d.ts +5 -0
  29. package/dist/endpoint.d.ts.map +1 -0
  30. package/dist/endpoint.js +65 -0
  31. package/dist/index.d.ts +14 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +13 -0
  34. package/dist/openapi.d.ts +3 -0
  35. package/dist/openapi.d.ts.map +1 -0
  36. package/dist/openapi.js +205 -0
  37. package/dist/pipeline.d.ts +77 -0
  38. package/dist/pipeline.d.ts.map +1 -0
  39. package/dist/pipeline.js +424 -0
  40. package/dist/query-logger.d.ts +65 -0
  41. package/dist/query-logger.d.ts.map +1 -0
  42. package/dist/query-logger.js +91 -0
  43. package/dist/router.d.ts +13 -0
  44. package/dist/router.d.ts.map +1 -0
  45. package/dist/router.js +56 -0
  46. package/dist/server/builder.d.ts +7 -0
  47. package/dist/server/builder.d.ts.map +1 -0
  48. package/dist/server/builder.js +73 -0
  49. package/dist/server/define-serve.d.ts +3 -0
  50. package/dist/server/define-serve.d.ts.map +1 -0
  51. package/dist/server/define-serve.js +88 -0
  52. package/dist/server/execute-query.d.ts +8 -0
  53. package/dist/server/execute-query.d.ts.map +1 -0
  54. package/dist/server/execute-query.js +39 -0
  55. package/dist/server/index.d.ts +6 -0
  56. package/dist/server/index.d.ts.map +1 -0
  57. package/dist/server/index.js +5 -0
  58. package/dist/server/init-serve.d.ts +8 -0
  59. package/dist/server/init-serve.d.ts.map +1 -0
  60. package/dist/server/init-serve.js +18 -0
  61. package/dist/server/mapper.d.ts +3 -0
  62. package/dist/server/mapper.d.ts.map +1 -0
  63. package/dist/server/mapper.js +30 -0
  64. package/dist/tenant.d.ts +35 -0
  65. package/dist/tenant.d.ts.map +1 -0
  66. package/dist/tenant.js +49 -0
  67. package/dist/type-tests/builder.test-d.d.ts +13 -0
  68. package/dist/type-tests/builder.test-d.d.ts.map +1 -0
  69. package/dist/type-tests/builder.test-d.js +20 -0
  70. package/dist/types.d.ts +514 -0
  71. package/dist/types.d.ts.map +1 -0
  72. package/dist/types.js +1 -0
  73. package/dist/utils.d.ts +4 -0
  74. package/dist/utils.d.ts.map +1 -0
  75. package/dist/utils.js +16 -0
  76. package/package.json +1 -1
@@ -0,0 +1,205 @@
1
+ import { zodToJsonSchema } from "zod-to-json-schema";
2
+ const ERROR_SCHEMA = {
3
+ type: "object",
4
+ properties: {
5
+ error: {
6
+ type: "object",
7
+ properties: {
8
+ type: { type: "string" },
9
+ message: { type: "string" },
10
+ details: { type: "object" },
11
+ },
12
+ required: ["type", "message"],
13
+ },
14
+ },
15
+ required: ["error"],
16
+ };
17
+ const dereferenceSchema = (schema) => {
18
+ if (!schema || typeof schema !== "object") {
19
+ return schema;
20
+ }
21
+ if (schema.$ref && schema.definitions) {
22
+ const refKey = String(schema.$ref).split("/").pop();
23
+ if (refKey && schema.definitions[refKey]) {
24
+ return schema.definitions[refKey];
25
+ }
26
+ }
27
+ return schema;
28
+ };
29
+ const removeDefinitions = (schema) => {
30
+ if (!schema || typeof schema !== "object") {
31
+ return schema;
32
+ }
33
+ // Handle arrays
34
+ if (Array.isArray(schema)) {
35
+ return schema.map(item => removeDefinitions(item));
36
+ }
37
+ // If this schema has a $ref and definitions, inline the definition
38
+ if (schema.$ref && schema.definitions) {
39
+ const refKey = String(schema.$ref).split("/").pop();
40
+ if (refKey && schema.definitions[refKey]) {
41
+ const resolved = schema.definitions[refKey];
42
+ delete schema.$ref;
43
+ delete schema.definitions;
44
+ return removeDefinitions({ ...schema, ...resolved });
45
+ }
46
+ }
47
+ // Remove definitions property if it exists
48
+ const { definitions: _definitions, $ref: _ref, ...rest } = schema;
49
+ // Recursively clean nested objects and arrays
50
+ const result = {};
51
+ for (const [key, value] of Object.entries(rest)) {
52
+ if (Array.isArray(value)) {
53
+ result[key] = value.map(item => typeof item === "object" && item !== null ? removeDefinitions(item) : item);
54
+ }
55
+ else if (typeof value === "object" && value !== null) {
56
+ result[key] = removeDefinitions(value);
57
+ }
58
+ else {
59
+ result[key] = value;
60
+ }
61
+ }
62
+ return result;
63
+ };
64
+ const toJsonSchema = (schema, name) => {
65
+ if (!schema) {
66
+ return { type: "object" };
67
+ }
68
+ const jsonSchema = zodToJsonSchema(schema, {
69
+ target: "openApi3",
70
+ name,
71
+ $refStrategy: "none",
72
+ });
73
+ return removeDefinitions(jsonSchema);
74
+ };
75
+ const toQueryParameters = (schema, name) => {
76
+ if (!schema) {
77
+ return [];
78
+ }
79
+ const jsonSchema = dereferenceSchema(toJsonSchema(schema, name));
80
+ if (!jsonSchema || typeof jsonSchema !== "object") {
81
+ return [];
82
+ }
83
+ const schemaType = jsonSchema.type;
84
+ const isObjectType = schemaType === "object" || (Array.isArray(schemaType) && schemaType.includes("object"));
85
+ if (!isObjectType || typeof jsonSchema.properties !== "object") {
86
+ return [];
87
+ }
88
+ const properties = jsonSchema.properties;
89
+ const requiredSet = new Set(Array.isArray(jsonSchema.required) ? jsonSchema.required : []);
90
+ return Object.entries(properties).map(([key, value]) => ({
91
+ name: key,
92
+ in: "query",
93
+ required: requiredSet.has(key),
94
+ schema: value,
95
+ }));
96
+ };
97
+ const toOperation = (endpoint, nameSuffix) => {
98
+ const operation = {
99
+ operationId: endpoint.key,
100
+ summary: endpoint.metadata.summary,
101
+ description: endpoint.metadata.description,
102
+ tags: endpoint.metadata.tags.length ? endpoint.metadata.tags : undefined,
103
+ responses: {
104
+ 200: {
105
+ description: "Successful response",
106
+ content: {
107
+ "application/json": {
108
+ schema: toJsonSchema(endpoint.outputSchema, `${endpoint.key}${nameSuffix}`),
109
+ },
110
+ },
111
+ },
112
+ default: {
113
+ description: "Error response",
114
+ content: {
115
+ "application/json": {
116
+ schema: ERROR_SCHEMA,
117
+ },
118
+ },
119
+ },
120
+ },
121
+ };
122
+ const queryParameters = endpoint.method === "GET"
123
+ ? toQueryParameters(endpoint.inputSchema, `${endpoint.key}Query`)
124
+ : [];
125
+ if (queryParameters.length > 0) {
126
+ operation.parameters = queryParameters;
127
+ }
128
+ else if (endpoint.inputSchema) {
129
+ operation.requestBody = {
130
+ required: true,
131
+ content: {
132
+ "application/json": {
133
+ schema: toJsonSchema(endpoint.inputSchema, `${endpoint.key}Request`),
134
+ },
135
+ },
136
+ };
137
+ }
138
+ if (endpoint.metadata.requiresAuth) {
139
+ operation.security = [{ ApiKeyAuth: [] }];
140
+ // Add auth guard information to description
141
+ const authDetails = [];
142
+ if (endpoint.requiredRoles && endpoint.requiredRoles.length > 0) {
143
+ authDetails.push(`**Required roles:** ${endpoint.requiredRoles.join(", ")}`);
144
+ }
145
+ if (endpoint.requiredScopes && endpoint.requiredScopes.length > 0) {
146
+ authDetails.push(`**Required scopes:** ${endpoint.requiredScopes.join(", ")}`);
147
+ }
148
+ if (authDetails.length > 0) {
149
+ operation.description = [
150
+ endpoint.metadata.description || "",
151
+ ...authDetails,
152
+ ]
153
+ .filter(Boolean)
154
+ .join("\n\n");
155
+ }
156
+ }
157
+ return operation;
158
+ };
159
+ const normalizeInfo = (options) => {
160
+ const info = options?.info;
161
+ return {
162
+ title: info?.title ?? "hypequery API",
163
+ version: options?.version ?? "1.0.0",
164
+ description: info?.description,
165
+ termsOfService: info?.termsOfService,
166
+ contact: info?.contact,
167
+ license: info?.license,
168
+ };
169
+ };
170
+ export const buildOpenApiDocument = (endpoints, options) => {
171
+ var _a;
172
+ const document = {
173
+ openapi: "3.1.0",
174
+ info: normalizeInfo(options),
175
+ servers: options?.servers ?? [],
176
+ paths: {},
177
+ };
178
+ let needsSecurityScheme = false;
179
+ for (const endpoint of endpoints) {
180
+ if (endpoint.metadata.visibility && endpoint.metadata.visibility !== "public") {
181
+ continue;
182
+ }
183
+ const path = endpoint.metadata.path || "/";
184
+ const method = endpoint.method.toLowerCase();
185
+ const pathItem = ((_a = document.paths)[path] ?? (_a[path] = {}));
186
+ const operation = toOperation(endpoint, "Response");
187
+ if (endpoint.metadata.requiresAuth) {
188
+ needsSecurityScheme = true;
189
+ }
190
+ pathItem[method] = operation;
191
+ }
192
+ if (needsSecurityScheme) {
193
+ document.components = {
194
+ ...(document.components ?? {}),
195
+ securitySchemes: {
196
+ ApiKeyAuth: {
197
+ type: "apiKey",
198
+ name: "Authorization",
199
+ in: "header",
200
+ },
201
+ },
202
+ };
203
+ }
204
+ return document;
205
+ };
@@ -0,0 +1,77 @@
1
+ import { z } from 'zod';
2
+ import type { AuthContext, AuthStrategy, DocsOptions, OpenApiOptions, ServeContextFactory, ServeEndpoint, ServeHandler, ServeLifecycleHooks, ServeMiddleware, ServeRequest, ServeResponse, TenantConfig } from './types.js';
3
+ import { ServeQueryLogger } from './query-logger.js';
4
+ export interface ExecuteEndpointOptions<TContext extends Record<string, unknown>, TAuth extends AuthContext> {
5
+ endpoint: ServeEndpoint<any, any, TContext, TAuth>;
6
+ request: ServeRequest;
7
+ requestId?: string;
8
+ authStrategies: AuthStrategy<TAuth>[];
9
+ contextFactory?: ServeContextFactory<TContext, TAuth>;
10
+ globalMiddlewares: ServeMiddleware<any, any, TContext, TAuth>[];
11
+ tenantConfig?: TenantConfig<TAuth>;
12
+ hooks?: ServeLifecycleHooks<TAuth>;
13
+ queryLogger?: ServeQueryLogger;
14
+ additionalContext?: Partial<TContext>;
15
+ verboseAuthErrors?: boolean;
16
+ }
17
+ export declare const executeEndpoint: <TContext extends Record<string, unknown>, TAuth extends AuthContext>(options: ExecuteEndpointOptions<TContext, TAuth>) => Promise<ServeResponse>;
18
+ interface HandlerOptions<TContext extends Record<string, unknown>, TAuth extends AuthContext> {
19
+ router: import('./router.js').ServeRouter;
20
+ globalMiddlewares: ServeMiddleware<any, any, TContext, TAuth>[];
21
+ authStrategies: AuthStrategy<TAuth>[];
22
+ tenantConfig?: TenantConfig<TAuth>;
23
+ contextFactory?: ServeContextFactory<TContext, TAuth>;
24
+ hooks?: ServeLifecycleHooks<TAuth>;
25
+ queryLogger?: ServeQueryLogger;
26
+ verboseAuthErrors?: boolean;
27
+ }
28
+ export declare const createServeHandler: <TContext extends Record<string, unknown>, TAuth extends AuthContext>({ router, globalMiddlewares, authStrategies, tenantConfig, contextFactory, hooks, queryLogger, verboseAuthErrors, }: HandlerOptions<TContext, TAuth>) => ServeHandler;
29
+ export declare const createOpenApiEndpoint: (path: string, getEndpoints: () => ServeEndpoint<any, any, any, any>[], options?: OpenApiOptions) => {
30
+ key: string;
31
+ method: "GET";
32
+ inputSchema: undefined;
33
+ outputSchema: z.ZodAny;
34
+ handler: () => Promise<unknown>;
35
+ query: undefined;
36
+ middlewares: never[];
37
+ auth: null;
38
+ metadata: {
39
+ path: string;
40
+ method: "GET";
41
+ name: string;
42
+ summary: string;
43
+ description: string;
44
+ tags: string[];
45
+ requiresAuth: false;
46
+ deprecated: false;
47
+ visibility: "internal";
48
+ };
49
+ cacheTtlMs: null;
50
+ };
51
+ export declare const createDocsEndpoint: (path: string, openapiPath: string, options?: DocsOptions) => {
52
+ key: string;
53
+ method: "GET";
54
+ inputSchema: undefined;
55
+ outputSchema: z.ZodString;
56
+ handler: () => Promise<string>;
57
+ query: undefined;
58
+ middlewares: never[];
59
+ auth: null;
60
+ metadata: {
61
+ path: string;
62
+ method: "GET";
63
+ name: string;
64
+ summary: string;
65
+ description: string;
66
+ tags: string[];
67
+ requiresAuth: false;
68
+ deprecated: false;
69
+ visibility: "internal";
70
+ };
71
+ cacheTtlMs: null;
72
+ defaultHeaders: {
73
+ 'content-type': string;
74
+ };
75
+ };
76
+ export {};
77
+ //# sourceMappingURL=pipeline.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pipeline.d.ts","sourceRoot":"","sources":["../src/pipeline.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAmB,MAAM,KAAK,CAAC;AAEzC,OAAO,KAAK,EACV,WAAW,EAEX,YAAY,EACZ,WAAW,EAKX,cAAc,EACd,mBAAmB,EACnB,aAAa,EACb,YAAY,EACZ,mBAAmB,EACnB,eAAe,EACf,YAAY,EACZ,aAAa,EACb,YAAY,EAEb,MAAM,YAAY,CAAC;AAKpB,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAsKrD,MAAM,WAAW,sBAAsB,CACrC,QAAQ,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACxC,KAAK,SAAS,WAAW;IAEzB,QAAQ,EAAE,aAAa,CAAC,GAAG,EAAE,GAAG,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;IACnD,OAAO,EAAE,YAAY,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,cAAc,EAAE,YAAY,CAAC,KAAK,CAAC,EAAE,CAAC;IACtC,cAAc,CAAC,EAAE,mBAAmB,CAAC,QAAQ,EAAE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