@hypequery/serve 0.0.5 → 0.0.7

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 (62) hide show
  1. package/LICENSE +201 -0
  2. package/package.json +9 -9
  3. package/dist/adapters/fetch.d.ts +0 -3
  4. package/dist/adapters/fetch.d.ts.map +0 -1
  5. package/dist/adapters/fetch.js +0 -26
  6. package/dist/adapters/node.d.ts +0 -8
  7. package/dist/adapters/node.d.ts.map +0 -1
  8. package/dist/adapters/node.js +0 -105
  9. package/dist/adapters/utils.d.ts +0 -39
  10. package/dist/adapters/utils.d.ts.map +0 -1
  11. package/dist/adapters/utils.js +0 -114
  12. package/dist/adapters/vercel.d.ts +0 -7
  13. package/dist/adapters/vercel.d.ts.map +0 -1
  14. package/dist/adapters/vercel.js +0 -13
  15. package/dist/auth.d.ts +0 -14
  16. package/dist/auth.d.ts.map +0 -1
  17. package/dist/auth.js +0 -37
  18. package/dist/builder.d.ts +0 -3
  19. package/dist/builder.d.ts.map +0 -1
  20. package/dist/builder.js +0 -41
  21. package/dist/client-config.d.ts +0 -44
  22. package/dist/client-config.d.ts.map +0 -1
  23. package/dist/client-config.js +0 -53
  24. package/dist/dev.d.ts +0 -9
  25. package/dist/dev.d.ts.map +0 -1
  26. package/dist/dev.js +0 -24
  27. package/dist/docs-ui.d.ts +0 -3
  28. package/dist/docs-ui.d.ts.map +0 -1
  29. package/dist/docs-ui.js +0 -34
  30. package/dist/endpoint.d.ts +0 -5
  31. package/dist/endpoint.d.ts.map +0 -1
  32. package/dist/endpoint.js +0 -59
  33. package/dist/index.d.ts +0 -13
  34. package/dist/index.d.ts.map +0 -1
  35. package/dist/index.js +0 -12
  36. package/dist/openapi.d.ts +0 -3
  37. package/dist/openapi.d.ts.map +0 -1
  38. package/dist/openapi.js +0 -189
  39. package/dist/pipeline.d.ts +0 -72
  40. package/dist/pipeline.d.ts.map +0 -1
  41. package/dist/pipeline.js +0 -317
  42. package/dist/query-logger.d.ts +0 -65
  43. package/dist/query-logger.d.ts.map +0 -1
  44. package/dist/query-logger.js +0 -91
  45. package/dist/router.d.ts +0 -13
  46. package/dist/router.d.ts.map +0 -1
  47. package/dist/router.js +0 -56
  48. package/dist/server.d.ts +0 -9
  49. package/dist/server.d.ts.map +0 -1
  50. package/dist/server.js +0 -191
  51. package/dist/tenant.d.ts +0 -35
  52. package/dist/tenant.d.ts.map +0 -1
  53. package/dist/tenant.js +0 -49
  54. package/dist/type-tests/builder.test-d.d.ts +0 -13
  55. package/dist/type-tests/builder.test-d.d.ts.map +0 -1
  56. package/dist/type-tests/builder.test-d.js +0 -20
  57. package/dist/types.d.ts +0 -373
  58. package/dist/types.d.ts.map +0 -1
  59. package/dist/types.js +0 -1
  60. package/dist/utils.d.ts +0 -4
  61. package/dist/utils.d.ts.map +0 -1
  62. package/dist/utils.js +0 -16
package/dist/server.js DELETED
@@ -1,191 +0,0 @@
1
- import { zodToJsonSchema } from "zod-to-json-schema";
2
- import { startNodeServer } from "./adapters/node.js";
3
- import { createEndpoint } from "./endpoint.js";
4
- import { applyBasePath, normalizeRoutePath, ServeRouter } from "./router.js";
5
- import { createProcedureBuilder } from "./builder.js";
6
- import { ensureArray, mergeTags } from "./utils.js";
7
- import { createDocsEndpoint, createOpenApiEndpoint, createServeHandler, executeEndpoint, } from "./pipeline.js";
8
- export const defineServe = (config) => {
9
- const basePath = config.basePath ?? "/api/analytics";
10
- const router = new ServeRouter(basePath);
11
- const globalMiddlewares = [
12
- ...(config.middlewares ?? []),
13
- ];
14
- const authStrategies = ensureArray(config.auth);
15
- const globalTenantConfig = config.tenant;
16
- const contextFactory = config.context;
17
- const hooks = (config.hooks ?? {});
18
- const openapiConfig = {
19
- enabled: config.openapi?.enabled ?? true,
20
- path: config.openapi?.path ?? "/openapi.json",
21
- };
22
- const docsConfig = {
23
- enabled: config.docs?.enabled ?? true,
24
- path: config.docs?.path ?? "/docs",
25
- };
26
- const openapiPublicPath = applyBasePath(basePath, openapiConfig.path);
27
- const configuredQueries = config.queries ?? {};
28
- const queryEntries = {};
29
- const registerQuery = (key, definition) => {
30
- queryEntries[key] = createEndpoint(String(key), definition);
31
- };
32
- for (const key of Object.keys(configuredQueries)) {
33
- registerQuery(key, configuredQueries[key]);
34
- }
35
- const handler = createServeHandler({
36
- router,
37
- globalMiddlewares,
38
- authStrategies,
39
- tenantConfig: globalTenantConfig,
40
- contextFactory,
41
- hooks,
42
- });
43
- // Track route configuration for client config extraction
44
- const routeConfig = {};
45
- const executeQuery = async (key, options) => {
46
- const endpoint = queryEntries[key];
47
- if (!endpoint) {
48
- throw new Error(`No query registered for key ${String(key)}`);
49
- }
50
- const request = {
51
- method: endpoint.method,
52
- path: options?.request?.path ?? endpoint.metadata.path ?? `/__execute/${String(key)}`,
53
- query: options?.request?.query ?? {},
54
- headers: options?.request?.headers ?? {},
55
- body: options?.input ?? options?.request?.body,
56
- raw: options?.request?.raw,
57
- };
58
- const response = await executeEndpoint({
59
- endpoint,
60
- request,
61
- authStrategies,
62
- contextFactory,
63
- globalMiddlewares,
64
- tenantConfig: globalTenantConfig,
65
- hooks,
66
- additionalContext: options?.context,
67
- });
68
- if (response.status !== 200) {
69
- const errorBody = response.body;
70
- const error = new Error(errorBody.error.message);
71
- error.type = errorBody.error.type;
72
- if (errorBody.error.details) {
73
- error.details = errorBody.error.details;
74
- }
75
- throw error;
76
- }
77
- return response.body;
78
- };
79
- const builder = {
80
- queries: queryEntries,
81
- _routeConfig: routeConfig,
82
- route: (path, endpoint, options) => {
83
- if (!endpoint) {
84
- throw new Error("Endpoint definition is required when registering a route");
85
- }
86
- const method = options?.method ?? endpoint.method;
87
- // Find the query key for this endpoint
88
- const queryKey = Object.entries(queryEntries).find(([_, e]) => e === endpoint)?.[0];
89
- if (queryKey) {
90
- routeConfig[queryKey] = { method };
91
- }
92
- const normalizedPath = normalizeRoutePath(path);
93
- const fallbackRequiresAuth = endpoint.auth
94
- ? true
95
- : authStrategies.length > 0
96
- ? true
97
- : undefined;
98
- const requiresAuth = options?.requiresAuth ?? endpoint.metadata.requiresAuth ?? fallbackRequiresAuth;
99
- const visibility = options?.visibility ?? endpoint.metadata.visibility ?? "public";
100
- const metadata = {
101
- ...endpoint.metadata,
102
- path: normalizedPath,
103
- method,
104
- name: options?.name ?? endpoint.metadata.name ?? endpoint.key,
105
- summary: options?.summary ?? endpoint.metadata.summary,
106
- description: options?.description ?? endpoint.metadata.description,
107
- tags: mergeTags(endpoint.metadata.tags, options?.tags),
108
- requiresAuth,
109
- visibility,
110
- };
111
- const middlewares = [...endpoint.middlewares, ...(options?.middlewares ?? [])];
112
- const registeredEndpoint = {
113
- ...endpoint,
114
- method,
115
- metadata,
116
- middlewares,
117
- };
118
- router.register(registeredEndpoint);
119
- return builder;
120
- },
121
- use: (middleware) => {
122
- globalMiddlewares.push(middleware);
123
- return builder;
124
- },
125
- useAuth: (strategy) => {
126
- authStrategies.push(strategy);
127
- router.markRoutesRequireAuth();
128
- return builder;
129
- },
130
- execute: executeQuery,
131
- run: executeQuery,
132
- describe: () => {
133
- const description = {
134
- basePath: basePath || undefined,
135
- queries: router.list().map(mapEndpointToToolkit),
136
- };
137
- return description;
138
- },
139
- handler,
140
- start: async (options) => startNodeServer(handler, options),
141
- };
142
- if (openapiConfig.enabled) {
143
- const openapiEndpoint = createOpenApiEndpoint(openapiConfig.path, () => router.list(), config.openapi);
144
- router.register(openapiEndpoint);
145
- }
146
- if (docsConfig.enabled) {
147
- const docsEndpoint = createDocsEndpoint(docsConfig.path, openapiPublicPath, config.docs);
148
- router.register(docsEndpoint);
149
- }
150
- return builder;
151
- };
152
- const mapEndpointToToolkit = (endpoint) => {
153
- // Use type assertion to avoid deep type instantiation issues with zodToJsonSchema
154
- const inputSchema = endpoint.inputSchema
155
- ? zodToJsonSchema(endpoint.inputSchema, { target: "openApi3" })
156
- : undefined;
157
- const outputSchema = endpoint.outputSchema
158
- ? zodToJsonSchema(endpoint.outputSchema, { target: "openApi3" })
159
- : undefined;
160
- return {
161
- key: endpoint.key,
162
- path: endpoint.metadata.path,
163
- method: endpoint.method,
164
- name: endpoint.metadata.name ?? endpoint.key,
165
- summary: endpoint.metadata.summary,
166
- description: endpoint.metadata.description,
167
- tags: endpoint.metadata.tags,
168
- visibility: endpoint.metadata.visibility,
169
- requiresAuth: Boolean(endpoint.metadata.requiresAuth),
170
- requiresTenant: endpoint.tenant ? (endpoint.tenant.required !== false) : undefined,
171
- inputSchema,
172
- outputSchema,
173
- custom: endpoint.metadata.custom,
174
- };
175
- };
176
- export const initServe = (options) => {
177
- const { context, ...staticOptions } = options;
178
- const procedure = createProcedureBuilder();
179
- return {
180
- procedure,
181
- query: procedure,
182
- queries: (definitions) => definitions,
183
- define: (config) => {
184
- return defineServe({
185
- ...staticOptions,
186
- ...config,
187
- context: (context ?? {}),
188
- });
189
- },
190
- };
191
- };
package/dist/tenant.d.ts DELETED
@@ -1,35 +0,0 @@
1
- /**
2
- * Utilities for multi-tenant query isolation
3
- */
4
- /**
5
- * Creates a tenant-scoped query builder wrapper that automatically
6
- * adds WHERE clauses to filter by tenant.
7
- *
8
- * @example
9
- * ```typescript
10
- * const api = defineServe({
11
- * context: ({ auth }) => ({
12
- * db: createTenantScope(myDb, {
13
- * tenantId: auth?.tenantId,
14
- * column: 'organization_id',
15
- * }),
16
- * }),
17
- * });
18
- * ```
19
- */
20
- export declare function createTenantScope<TDb extends {
21
- table: (name: string) => any;
22
- }>(db: TDb, options: {
23
- tenantId: string | null | undefined;
24
- column: string;
25
- }): TDb;
26
- /**
27
- * Runtime warning when tenant isolation might be misconfigured
28
- */
29
- export declare function warnTenantMisconfiguration(options: {
30
- queryKey: string;
31
- hasTenantConfig: boolean;
32
- hasTenantId: boolean;
33
- mode?: string;
34
- }): void;
35
- //# sourceMappingURL=tenant.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"tenant.d.ts","sourceRoot":"","sources":["../src/tenant.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,SAAS;IAAE,KAAK,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,GAAG,CAAA;CAAE,EAC5E,EAAE,EAAE,GAAG,EACP,OAAO,EAAE;IACP,QAAQ,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;IACpC,MAAM,EAAE,MAAM,CAAC;CAChB,GACA,GAAG,CAoBL;AAED;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,OAAO,EAAE;IAClD,QAAQ,EAAE,MAAM,CAAC;IACjB,eAAe,EAAE,OAAO,CAAC;IACzB,WAAW,EAAE,OAAO,CAAC;IACrB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,QAYA"}
package/dist/tenant.js DELETED
@@ -1,49 +0,0 @@
1
- /**
2
- * Utilities for multi-tenant query isolation
3
- */
4
- /**
5
- * Creates a tenant-scoped query builder wrapper that automatically
6
- * adds WHERE clauses to filter by tenant.
7
- *
8
- * @example
9
- * ```typescript
10
- * const api = defineServe({
11
- * context: ({ auth }) => ({
12
- * db: createTenantScope(myDb, {
13
- * tenantId: auth?.tenantId,
14
- * column: 'organization_id',
15
- * }),
16
- * }),
17
- * });
18
- * ```
19
- */
20
- export function createTenantScope(db, options) {
21
- if (!options.tenantId) {
22
- return db;
23
- }
24
- const originalTable = db.table.bind(db);
25
- return {
26
- ...db,
27
- table: (name) => {
28
- const query = originalTable(name);
29
- // Auto-inject tenant filter
30
- if (query && typeof query.where === 'function') {
31
- return query.where(options.column, 'eq', options.tenantId);
32
- }
33
- return query;
34
- },
35
- };
36
- }
37
- /**
38
- * Runtime warning when tenant isolation might be misconfigured
39
- */
40
- export function warnTenantMisconfiguration(options) {
41
- if (!options.hasTenantConfig) {
42
- console.warn(`[hypequery/serve] Query "${options.queryKey}" accesses user data but has no tenant configuration. ` +
43
- `This may lead to data leaks. Add tenant config to defineServe or the query definition.`);
44
- }
45
- else if (options.hasTenantId && options.mode === 'manual') {
46
- console.warn(`[hypequery/serve] Query "${options.queryKey}" uses manual tenant mode. ` +
47
- `Ensure you manually filter queries by tenantId to prevent data leaks.`);
48
- }
49
- }
@@ -1,13 +0,0 @@
1
- import { z } from 'zod';
2
- export declare const api: import("../types.js").ServeBuilder<import("../types.js").ServeEndpointMap<{
3
- typedQuery: import("../types.js").ServeQueryConfig<z.ZodObject<{
4
- plan: z.ZodOptional<z.ZodString>;
5
- }, "strip", z.ZodTypeAny, {
6
- plan?: string | undefined;
7
- }, {
8
- plan?: string | undefined;
9
- }>, z.ZodTypeAny, {}, import("../types.js").AuthContext, {
10
- plan: string;
11
- }[]>;
12
- }, {}, import("../types.js").AuthContext>, {}, import("../types.js").AuthContext>;
13
- //# sourceMappingURL=builder.test-d.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"builder.test-d.d.ts","sourceRoot":"","sources":["../../src/type-tests/builder.test-d.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAUxB,eAAO,MAAM,GAAG;;;;;;;;;;iFAUd,CAAC"}
@@ -1,20 +0,0 @@
1
- import { initServe } from '../index.js';
2
- import { z } from 'zod';
3
- const serve = initServe({
4
- context: () => ({}),
5
- });
6
- const { query } = serve;
7
- export const api = serve.define({
8
- queries: serve.queries({
9
- typedQuery: query
10
- .describe('builder infers input + output')
11
- .input(z.object({ plan: z.string().optional() }))
12
- .query(async ({ input }) => {
13
- const plan = input.plan ?? 'starter';
14
- return [{ plan }];
15
- }),
16
- }),
17
- });
18
- const _resultIsTyped = [{ plan: 'starter' }];
19
- // @ts-expect-error plan must be string
20
- const _resultRejectsNumber = [{ plan: 123 }];