omni-rest 0.1.0
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/LICENSE +21 -0
- package/README.md +476 -0
- package/dist/adapters/express.d.mts +34 -0
- package/dist/adapters/express.d.ts +34 -0
- package/dist/adapters/express.js +450 -0
- package/dist/adapters/express.js.map +1 -0
- package/dist/adapters/express.mjs +448 -0
- package/dist/adapters/express.mjs.map +1 -0
- package/dist/adapters/fastify.d.mts +26 -0
- package/dist/adapters/fastify.d.ts +26 -0
- package/dist/adapters/fastify.js +438 -0
- package/dist/adapters/fastify.js.map +1 -0
- package/dist/adapters/fastify.mjs +436 -0
- package/dist/adapters/fastify.mjs.map +1 -0
- package/dist/adapters/nextjs.d.mts +41 -0
- package/dist/adapters/nextjs.d.ts +41 -0
- package/dist/adapters/nextjs.js +426 -0
- package/dist/adapters/nextjs.js.map +1 -0
- package/dist/adapters/nextjs.mjs +424 -0
- package/dist/adapters/nextjs.mjs.map +1 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +519 -0
- package/dist/cli.js.map +1 -0
- package/dist/cli.mjs +512 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/index.d.mts +132 -0
- package/dist/index.d.ts +132 -0
- package/dist/index.js +1023 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1006 -0
- package/dist/index.mjs.map +1 -0
- package/dist/types-CzjpYtyN.d.mts +71 -0
- package/dist/types-CzjpYtyN.d.ts +71 -0
- package/package.json +91 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { PrismaClient } from '@prisma/client';
|
|
2
|
+
import { P as PrismaRestOptions, R as RouterInstance, M as ModelMeta, a as ParsedQuery, G as GuardMap, H as HookFn, b as HookContext } from './types-CzjpYtyN.mjs';
|
|
3
|
+
export { F as FieldMeta, c as GuardFn, d as HandlerResult } from './types-CzjpYtyN.mjs';
|
|
4
|
+
export { expressAdapter } from './adapters/express.mjs';
|
|
5
|
+
export { nextjsAdapter } from './adapters/nextjs.mjs';
|
|
6
|
+
export { fastifyAdapter } from './adapters/fastify.mjs';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Creates a framework-agnostic CRUD router powered by Prisma DMMF.
|
|
10
|
+
*
|
|
11
|
+
* All adapters (Express, Next.js, Fastify) use this under the hood.
|
|
12
|
+
*/
|
|
13
|
+
declare function createRouter(prisma: PrismaClient, options?: PrismaRestOptions): RouterInstance;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Reads Prisma's DMMF (Data Model Meta Format) at runtime
|
|
17
|
+
* and returns structured metadata for every model in your schema.
|
|
18
|
+
*
|
|
19
|
+
* No file reading. No code generation. Pure runtime introspection.
|
|
20
|
+
*/
|
|
21
|
+
declare function getModels(prisma?: any): ModelMeta[];
|
|
22
|
+
/**
|
|
23
|
+
* Converts a Prisma model name to a URL-safe route segment.
|
|
24
|
+
* "UserProfile" → "userprofile"
|
|
25
|
+
* "OrderItem" → "orderitem"
|
|
26
|
+
*/
|
|
27
|
+
declare function toRouteName(modelName: string): string;
|
|
28
|
+
/**
|
|
29
|
+
* Returns a map of routeName → ModelMeta for O(1) lookups.
|
|
30
|
+
*/
|
|
31
|
+
declare function buildModelMap(models: ModelMeta[], allowList?: string[]): Record<string, ModelMeta>;
|
|
32
|
+
/**
|
|
33
|
+
* Gets the Prisma client delegate for a model.
|
|
34
|
+
* prisma["userProfile"] or prisma["user"] — handles camelCase.
|
|
35
|
+
*/
|
|
36
|
+
declare function getDelegate(prisma: any, meta: ModelMeta): any;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Parses URLSearchParams into a full Prisma query object.
|
|
40
|
+
*
|
|
41
|
+
* Supports:
|
|
42
|
+
* Filtering → ?name=John ?age_gte=18 ?status_in=a,b
|
|
43
|
+
* Sorting → ?sort=createdAt:desc or ?sort=name:asc
|
|
44
|
+
* Pagination → ?page=2&limit=10
|
|
45
|
+
* Relations → ?include=posts,profile
|
|
46
|
+
* Fields → ?select=id,name,email
|
|
47
|
+
*/
|
|
48
|
+
declare function buildQuery(searchParams: URLSearchParams, defaultLimit?: number, maxLimit?: number): ParsedQuery;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Runs the guard for the given model+method combo.
|
|
52
|
+
* Returns an error string if blocked, null if allowed.
|
|
53
|
+
*/
|
|
54
|
+
declare function runGuard(guards: GuardMap, model: string, method: string, ctx: {
|
|
55
|
+
id?: string | null;
|
|
56
|
+
body?: any;
|
|
57
|
+
}): Promise<string | null>;
|
|
58
|
+
/**
|
|
59
|
+
* Runs a lifecycle hook (beforeOperation / afterOperation).
|
|
60
|
+
* Silently swallows errors so hooks never crash the main flow.
|
|
61
|
+
*/
|
|
62
|
+
declare function runHook(hook: HookFn | undefined, ctx: HookContext): Promise<void>;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Generates a complete Zod schema file for ALL models in your Prisma schema.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* import { generateZodSchemas } from "omni-rest";
|
|
70
|
+
* const code = generateZodSchemas();
|
|
71
|
+
* fs.writeFileSync("src/schemas.ts", code);
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
declare function generateZodSchemas(): string;
|
|
75
|
+
/**
|
|
76
|
+
* Returns Zod schema OBJECTS at runtime (not source code).
|
|
77
|
+
* Useful for request validation in middleware.
|
|
78
|
+
*
|
|
79
|
+
* Requires "zod" to be installed in the host project.
|
|
80
|
+
*/
|
|
81
|
+
declare function buildRuntimeSchemas(): Record<string, {
|
|
82
|
+
create: any;
|
|
83
|
+
update: any;
|
|
84
|
+
}>;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Validates a request body against the auto-generated Zod schema for a model.
|
|
88
|
+
*
|
|
89
|
+
* Returns null if valid, or an error message string if invalid.
|
|
90
|
+
*
|
|
91
|
+
* @param modelRouteName e.g. "department"
|
|
92
|
+
* @param method HTTP method — POST uses createSchema, PUT/PATCH uses updateSchema
|
|
93
|
+
* @param body Request body object
|
|
94
|
+
*/
|
|
95
|
+
declare function validateBody(modelRouteName: string, method: string, body: any): string | null;
|
|
96
|
+
/**
|
|
97
|
+
* Creates a guard function that validates the request body automatically.
|
|
98
|
+
* Plug this into the guards option to get free validation.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* import { withValidation } from "omni-rest";
|
|
103
|
+
*
|
|
104
|
+
* expressAdapter(prisma, {
|
|
105
|
+
* guards: withValidation(), // validates ALL models
|
|
106
|
+
* });
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
declare function withValidation(overrides?: PrismaRestOptions["guards"]): GuardMap;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Generates a full OpenAPI 3.0 specification object for all exposed models.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```ts
|
|
116
|
+
* import { generateOpenApiSpec } from "omni-rest";
|
|
117
|
+
* const spec = generateOpenApiSpec(prisma, { title: "My API", version: "1.0.0", basePath: "/api" });
|
|
118
|
+
* fs.writeFileSync("openapi.json", JSON.stringify(spec, null, 2));
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
declare function generateOpenApiSpec(prisma: any, options?: {
|
|
122
|
+
title?: string;
|
|
123
|
+
version?: string;
|
|
124
|
+
basePath?: string;
|
|
125
|
+
allow?: string[];
|
|
126
|
+
servers?: {
|
|
127
|
+
url: string;
|
|
128
|
+
description?: string;
|
|
129
|
+
}[];
|
|
130
|
+
}): object;
|
|
131
|
+
|
|
132
|
+
export { GuardMap, HookContext, HookFn, ModelMeta, ParsedQuery, PrismaRestOptions, RouterInstance, buildModelMap, buildQuery, buildRuntimeSchemas, createRouter, generateOpenApiSpec, generateZodSchemas, getDelegate, getModels, runGuard, runHook, toRouteName, validateBody, withValidation };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { PrismaClient } from '@prisma/client';
|
|
2
|
+
import { P as PrismaRestOptions, R as RouterInstance, M as ModelMeta, a as ParsedQuery, G as GuardMap, H as HookFn, b as HookContext } from './types-CzjpYtyN.js';
|
|
3
|
+
export { F as FieldMeta, c as GuardFn, d as HandlerResult } from './types-CzjpYtyN.js';
|
|
4
|
+
export { expressAdapter } from './adapters/express.js';
|
|
5
|
+
export { nextjsAdapter } from './adapters/nextjs.js';
|
|
6
|
+
export { fastifyAdapter } from './adapters/fastify.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Creates a framework-agnostic CRUD router powered by Prisma DMMF.
|
|
10
|
+
*
|
|
11
|
+
* All adapters (Express, Next.js, Fastify) use this under the hood.
|
|
12
|
+
*/
|
|
13
|
+
declare function createRouter(prisma: PrismaClient, options?: PrismaRestOptions): RouterInstance;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Reads Prisma's DMMF (Data Model Meta Format) at runtime
|
|
17
|
+
* and returns structured metadata for every model in your schema.
|
|
18
|
+
*
|
|
19
|
+
* No file reading. No code generation. Pure runtime introspection.
|
|
20
|
+
*/
|
|
21
|
+
declare function getModels(prisma?: any): ModelMeta[];
|
|
22
|
+
/**
|
|
23
|
+
* Converts a Prisma model name to a URL-safe route segment.
|
|
24
|
+
* "UserProfile" → "userprofile"
|
|
25
|
+
* "OrderItem" → "orderitem"
|
|
26
|
+
*/
|
|
27
|
+
declare function toRouteName(modelName: string): string;
|
|
28
|
+
/**
|
|
29
|
+
* Returns a map of routeName → ModelMeta for O(1) lookups.
|
|
30
|
+
*/
|
|
31
|
+
declare function buildModelMap(models: ModelMeta[], allowList?: string[]): Record<string, ModelMeta>;
|
|
32
|
+
/**
|
|
33
|
+
* Gets the Prisma client delegate for a model.
|
|
34
|
+
* prisma["userProfile"] or prisma["user"] — handles camelCase.
|
|
35
|
+
*/
|
|
36
|
+
declare function getDelegate(prisma: any, meta: ModelMeta): any;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Parses URLSearchParams into a full Prisma query object.
|
|
40
|
+
*
|
|
41
|
+
* Supports:
|
|
42
|
+
* Filtering → ?name=John ?age_gte=18 ?status_in=a,b
|
|
43
|
+
* Sorting → ?sort=createdAt:desc or ?sort=name:asc
|
|
44
|
+
* Pagination → ?page=2&limit=10
|
|
45
|
+
* Relations → ?include=posts,profile
|
|
46
|
+
* Fields → ?select=id,name,email
|
|
47
|
+
*/
|
|
48
|
+
declare function buildQuery(searchParams: URLSearchParams, defaultLimit?: number, maxLimit?: number): ParsedQuery;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Runs the guard for the given model+method combo.
|
|
52
|
+
* Returns an error string if blocked, null if allowed.
|
|
53
|
+
*/
|
|
54
|
+
declare function runGuard(guards: GuardMap, model: string, method: string, ctx: {
|
|
55
|
+
id?: string | null;
|
|
56
|
+
body?: any;
|
|
57
|
+
}): Promise<string | null>;
|
|
58
|
+
/**
|
|
59
|
+
* Runs a lifecycle hook (beforeOperation / afterOperation).
|
|
60
|
+
* Silently swallows errors so hooks never crash the main flow.
|
|
61
|
+
*/
|
|
62
|
+
declare function runHook(hook: HookFn | undefined, ctx: HookContext): Promise<void>;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Generates a complete Zod schema file for ALL models in your Prisma schema.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* import { generateZodSchemas } from "omni-rest";
|
|
70
|
+
* const code = generateZodSchemas();
|
|
71
|
+
* fs.writeFileSync("src/schemas.ts", code);
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
declare function generateZodSchemas(): string;
|
|
75
|
+
/**
|
|
76
|
+
* Returns Zod schema OBJECTS at runtime (not source code).
|
|
77
|
+
* Useful for request validation in middleware.
|
|
78
|
+
*
|
|
79
|
+
* Requires "zod" to be installed in the host project.
|
|
80
|
+
*/
|
|
81
|
+
declare function buildRuntimeSchemas(): Record<string, {
|
|
82
|
+
create: any;
|
|
83
|
+
update: any;
|
|
84
|
+
}>;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Validates a request body against the auto-generated Zod schema for a model.
|
|
88
|
+
*
|
|
89
|
+
* Returns null if valid, or an error message string if invalid.
|
|
90
|
+
*
|
|
91
|
+
* @param modelRouteName e.g. "department"
|
|
92
|
+
* @param method HTTP method — POST uses createSchema, PUT/PATCH uses updateSchema
|
|
93
|
+
* @param body Request body object
|
|
94
|
+
*/
|
|
95
|
+
declare function validateBody(modelRouteName: string, method: string, body: any): string | null;
|
|
96
|
+
/**
|
|
97
|
+
* Creates a guard function that validates the request body automatically.
|
|
98
|
+
* Plug this into the guards option to get free validation.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* import { withValidation } from "omni-rest";
|
|
103
|
+
*
|
|
104
|
+
* expressAdapter(prisma, {
|
|
105
|
+
* guards: withValidation(), // validates ALL models
|
|
106
|
+
* });
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
declare function withValidation(overrides?: PrismaRestOptions["guards"]): GuardMap;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Generates a full OpenAPI 3.0 specification object for all exposed models.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```ts
|
|
116
|
+
* import { generateOpenApiSpec } from "omni-rest";
|
|
117
|
+
* const spec = generateOpenApiSpec(prisma, { title: "My API", version: "1.0.0", basePath: "/api" });
|
|
118
|
+
* fs.writeFileSync("openapi.json", JSON.stringify(spec, null, 2));
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
declare function generateOpenApiSpec(prisma: any, options?: {
|
|
122
|
+
title?: string;
|
|
123
|
+
version?: string;
|
|
124
|
+
basePath?: string;
|
|
125
|
+
allow?: string[];
|
|
126
|
+
servers?: {
|
|
127
|
+
url: string;
|
|
128
|
+
description?: string;
|
|
129
|
+
}[];
|
|
130
|
+
}): object;
|
|
131
|
+
|
|
132
|
+
export { GuardMap, HookContext, HookFn, ModelMeta, ParsedQuery, PrismaRestOptions, RouterInstance, buildModelMap, buildQuery, buildRuntimeSchemas, createRouter, generateOpenApiSpec, generateZodSchemas, getDelegate, getModels, runGuard, runHook, toRouteName, validateBody, withValidation };
|