say-under-me 0.0.2 → 0.0.4
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/dist/index.d.mts +14 -1
- package/dist/index.d.ts +14 -1
- package/dist/index.js +87 -3
- package/dist/index.mjs +86 -3
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
+
|
|
1
3
|
type sayHelloProps = {
|
|
2
4
|
firstName: string;
|
|
3
5
|
lastName?: string;
|
|
@@ -60,4 +62,15 @@ declare class PrismaCrud<T> {
|
|
|
60
62
|
private parseId;
|
|
61
63
|
}
|
|
62
64
|
|
|
63
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Creates a set of Next.js App Router handlers (GET, POST, PATCH, DELETE)
|
|
67
|
+
* for a given Prisma model
|
|
68
|
+
*/
|
|
69
|
+
declare function createApiHandler<T>(crud: PrismaCrud<T>): {
|
|
70
|
+
GET: (req: NextRequest, context: any) => Promise<NextResponse<any>>;
|
|
71
|
+
POST: (req: NextRequest) => Promise<NextResponse<any>>;
|
|
72
|
+
PATCH: (req: NextRequest, context: any) => Promise<NextResponse<any>>;
|
|
73
|
+
DELETE: (req: NextRequest, context: any) => Promise<NextResponse<unknown>>;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export { type ParseOptions, PrismaCrud, type PrismaQuery, createApiHandler, parsePrismaQuery, sayHello, type sayHelloProps };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
+
|
|
1
3
|
type sayHelloProps = {
|
|
2
4
|
firstName: string;
|
|
3
5
|
lastName?: string;
|
|
@@ -60,4 +62,15 @@ declare class PrismaCrud<T> {
|
|
|
60
62
|
private parseId;
|
|
61
63
|
}
|
|
62
64
|
|
|
63
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Creates a set of Next.js App Router handlers (GET, POST, PATCH, DELETE)
|
|
67
|
+
* for a given Prisma model
|
|
68
|
+
*/
|
|
69
|
+
declare function createApiHandler<T>(crud: PrismaCrud<T>): {
|
|
70
|
+
GET: (req: NextRequest, context: any) => Promise<NextResponse<any>>;
|
|
71
|
+
POST: (req: NextRequest) => Promise<NextResponse<any>>;
|
|
72
|
+
PATCH: (req: NextRequest, context: any) => Promise<NextResponse<any>>;
|
|
73
|
+
DELETE: (req: NextRequest, context: any) => Promise<NextResponse<unknown>>;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export { type ParseOptions, PrismaCrud, type PrismaQuery, createApiHandler, parsePrismaQuery, sayHello, type sayHelloProps };
|
package/dist/index.js
CHANGED
|
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
PrismaCrud: () => PrismaCrud,
|
|
24
|
+
createApiHandler: () => createApiHandler,
|
|
24
25
|
parsePrismaQuery: () => parsePrismaQuery,
|
|
25
26
|
sayHello: () => sayHello
|
|
26
27
|
});
|
|
@@ -108,9 +109,34 @@ function parsePrismaQuery(queryString, options = {}) {
|
|
|
108
109
|
}
|
|
109
110
|
function parseSelect(selectStr) {
|
|
110
111
|
const result = {};
|
|
111
|
-
selectStr
|
|
112
|
-
|
|
113
|
-
|
|
112
|
+
if (!selectStr) return result;
|
|
113
|
+
let depth = 0;
|
|
114
|
+
let current = "";
|
|
115
|
+
const fields = [];
|
|
116
|
+
for (let i = 0; i < selectStr.length; i++) {
|
|
117
|
+
const char = selectStr[i];
|
|
118
|
+
if (char === "(") depth++;
|
|
119
|
+
if (char === ")") depth--;
|
|
120
|
+
if (char === "," && depth === 0) {
|
|
121
|
+
fields.push(current.trim());
|
|
122
|
+
current = "";
|
|
123
|
+
} else {
|
|
124
|
+
current += char;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (current) fields.push(current.trim());
|
|
128
|
+
fields.forEach((field) => {
|
|
129
|
+
if (field.includes("(")) {
|
|
130
|
+
const start = field.indexOf("(");
|
|
131
|
+
const end = field.lastIndexOf(")");
|
|
132
|
+
const key = field.substring(0, start).trim();
|
|
133
|
+
const nestedContent = field.substring(start + 1, end).trim();
|
|
134
|
+
result[key] = {
|
|
135
|
+
select: parseSelect(nestedContent)
|
|
136
|
+
};
|
|
137
|
+
} else {
|
|
138
|
+
result[field] = true;
|
|
139
|
+
}
|
|
114
140
|
});
|
|
115
141
|
return result;
|
|
116
142
|
}
|
|
@@ -210,9 +236,67 @@ var PrismaCrud = class {
|
|
|
210
236
|
return id;
|
|
211
237
|
}
|
|
212
238
|
};
|
|
239
|
+
|
|
240
|
+
// src/adapters/next-handler.ts
|
|
241
|
+
var import_server = require("next/server");
|
|
242
|
+
function createApiHandler(crud) {
|
|
243
|
+
const GET = async (req, context) => {
|
|
244
|
+
try {
|
|
245
|
+
const params = await context.params;
|
|
246
|
+
const id = params?.id;
|
|
247
|
+
const { searchParams } = new URL(req.url);
|
|
248
|
+
const queryString = searchParams.toString();
|
|
249
|
+
if (id) {
|
|
250
|
+
const data2 = await crud.getById(id, queryString);
|
|
251
|
+
if (!data2) {
|
|
252
|
+
return import_server.NextResponse.json({ error: "Record not found" }, { status: 404 });
|
|
253
|
+
}
|
|
254
|
+
return import_server.NextResponse.json(data2);
|
|
255
|
+
}
|
|
256
|
+
const data = await crud.getAll(queryString);
|
|
257
|
+
return import_server.NextResponse.json(data);
|
|
258
|
+
} catch (error) {
|
|
259
|
+
return import_server.NextResponse.json({ error: error.message }, { status: 500 });
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
const POST = async (req) => {
|
|
263
|
+
try {
|
|
264
|
+
const body = await req.json();
|
|
265
|
+
const data = await crud.create(body);
|
|
266
|
+
return import_server.NextResponse.json(data, { status: 201 });
|
|
267
|
+
} catch (error) {
|
|
268
|
+
return import_server.NextResponse.json({ error: error.message }, { status: 400 });
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
const PATCH = async (req, context) => {
|
|
272
|
+
try {
|
|
273
|
+
const param = await context.params;
|
|
274
|
+
const id = param?.id;
|
|
275
|
+
if (!id) return import_server.NextResponse.json({ error: "ID is required" }, { status: 400 });
|
|
276
|
+
const body = await req.json();
|
|
277
|
+
const data = await crud.update(id, body);
|
|
278
|
+
return import_server.NextResponse.json(data);
|
|
279
|
+
} catch (error) {
|
|
280
|
+
return import_server.NextResponse.json({ error: error.message }, { status: 400 });
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
const DELETE = async (req, context) => {
|
|
284
|
+
try {
|
|
285
|
+
const params = await context.params;
|
|
286
|
+
const id = params?.id;
|
|
287
|
+
if (!id) return import_server.NextResponse.json({ error: "ID is required" }, { status: 400 });
|
|
288
|
+
await crud.delete(id);
|
|
289
|
+
return new import_server.NextResponse(null, { status: 204 });
|
|
290
|
+
} catch (error) {
|
|
291
|
+
return import_server.NextResponse.json({ error: error.message }, { status: 400 });
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
return { GET, POST, PATCH, DELETE };
|
|
295
|
+
}
|
|
213
296
|
// Annotate the CommonJS export names for ESM import in node:
|
|
214
297
|
0 && (module.exports = {
|
|
215
298
|
PrismaCrud,
|
|
299
|
+
createApiHandler,
|
|
216
300
|
parsePrismaQuery,
|
|
217
301
|
sayHello
|
|
218
302
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -80,9 +80,34 @@ function parsePrismaQuery(queryString, options = {}) {
|
|
|
80
80
|
}
|
|
81
81
|
function parseSelect(selectStr) {
|
|
82
82
|
const result = {};
|
|
83
|
-
selectStr
|
|
84
|
-
|
|
85
|
-
|
|
83
|
+
if (!selectStr) return result;
|
|
84
|
+
let depth = 0;
|
|
85
|
+
let current = "";
|
|
86
|
+
const fields = [];
|
|
87
|
+
for (let i = 0; i < selectStr.length; i++) {
|
|
88
|
+
const char = selectStr[i];
|
|
89
|
+
if (char === "(") depth++;
|
|
90
|
+
if (char === ")") depth--;
|
|
91
|
+
if (char === "," && depth === 0) {
|
|
92
|
+
fields.push(current.trim());
|
|
93
|
+
current = "";
|
|
94
|
+
} else {
|
|
95
|
+
current += char;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (current) fields.push(current.trim());
|
|
99
|
+
fields.forEach((field) => {
|
|
100
|
+
if (field.includes("(")) {
|
|
101
|
+
const start = field.indexOf("(");
|
|
102
|
+
const end = field.lastIndexOf(")");
|
|
103
|
+
const key = field.substring(0, start).trim();
|
|
104
|
+
const nestedContent = field.substring(start + 1, end).trim();
|
|
105
|
+
result[key] = {
|
|
106
|
+
select: parseSelect(nestedContent)
|
|
107
|
+
};
|
|
108
|
+
} else {
|
|
109
|
+
result[field] = true;
|
|
110
|
+
}
|
|
86
111
|
});
|
|
87
112
|
return result;
|
|
88
113
|
}
|
|
@@ -182,8 +207,66 @@ var PrismaCrud = class {
|
|
|
182
207
|
return id;
|
|
183
208
|
}
|
|
184
209
|
};
|
|
210
|
+
|
|
211
|
+
// src/adapters/next-handler.ts
|
|
212
|
+
import { NextResponse } from "next/server";
|
|
213
|
+
function createApiHandler(crud) {
|
|
214
|
+
const GET = async (req, context) => {
|
|
215
|
+
try {
|
|
216
|
+
const params = await context.params;
|
|
217
|
+
const id = params?.id;
|
|
218
|
+
const { searchParams } = new URL(req.url);
|
|
219
|
+
const queryString = searchParams.toString();
|
|
220
|
+
if (id) {
|
|
221
|
+
const data2 = await crud.getById(id, queryString);
|
|
222
|
+
if (!data2) {
|
|
223
|
+
return NextResponse.json({ error: "Record not found" }, { status: 404 });
|
|
224
|
+
}
|
|
225
|
+
return NextResponse.json(data2);
|
|
226
|
+
}
|
|
227
|
+
const data = await crud.getAll(queryString);
|
|
228
|
+
return NextResponse.json(data);
|
|
229
|
+
} catch (error) {
|
|
230
|
+
return NextResponse.json({ error: error.message }, { status: 500 });
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
const POST = async (req) => {
|
|
234
|
+
try {
|
|
235
|
+
const body = await req.json();
|
|
236
|
+
const data = await crud.create(body);
|
|
237
|
+
return NextResponse.json(data, { status: 201 });
|
|
238
|
+
} catch (error) {
|
|
239
|
+
return NextResponse.json({ error: error.message }, { status: 400 });
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
const PATCH = async (req, context) => {
|
|
243
|
+
try {
|
|
244
|
+
const param = await context.params;
|
|
245
|
+
const id = param?.id;
|
|
246
|
+
if (!id) return NextResponse.json({ error: "ID is required" }, { status: 400 });
|
|
247
|
+
const body = await req.json();
|
|
248
|
+
const data = await crud.update(id, body);
|
|
249
|
+
return NextResponse.json(data);
|
|
250
|
+
} catch (error) {
|
|
251
|
+
return NextResponse.json({ error: error.message }, { status: 400 });
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
const DELETE = async (req, context) => {
|
|
255
|
+
try {
|
|
256
|
+
const params = await context.params;
|
|
257
|
+
const id = params?.id;
|
|
258
|
+
if (!id) return NextResponse.json({ error: "ID is required" }, { status: 400 });
|
|
259
|
+
await crud.delete(id);
|
|
260
|
+
return new NextResponse(null, { status: 204 });
|
|
261
|
+
} catch (error) {
|
|
262
|
+
return NextResponse.json({ error: error.message }, { status: 400 });
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
return { GET, POST, PATCH, DELETE };
|
|
266
|
+
}
|
|
185
267
|
export {
|
|
186
268
|
PrismaCrud,
|
|
269
|
+
createApiHandler,
|
|
187
270
|
parsePrismaQuery,
|
|
188
271
|
sayHello
|
|
189
272
|
};
|