@shware/http 0.2.18 → 0.2.20
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/hono/handler.cjs +1 -1
- package/dist/hono/handler.cjs.map +1 -1
- package/dist/hono/handler.mjs +1 -1
- package/dist/hono/handler.mjs.map +1 -1
- package/dist/index.cjs +3 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/dist/response.cjs +57 -0
- package/dist/response.cjs.map +1 -0
- package/dist/response.d.cts +69 -0
- package/dist/response.d.ts +69 -0
- package/dist/response.mjs +31 -0
- package/dist/response.mjs.map +1 -0
- package/package.json +1 -1
package/dist/hono/handler.cjs
CHANGED
|
@@ -27,7 +27,7 @@ var import_status = require("../status.cjs");
|
|
|
27
27
|
var import_detail = require("../detail.cjs");
|
|
28
28
|
function errorHandler(error, c) {
|
|
29
29
|
const requestId = c.get("requestId");
|
|
30
|
-
const servingData = `${c.req.method}: ${c.req.
|
|
30
|
+
const servingData = `${c.req.method}: ${c.req.path}`;
|
|
31
31
|
const details = import_detail.Details.new().requestInfo({ requestId, servingData });
|
|
32
32
|
if (error instanceof import_status.StatusError) {
|
|
33
33
|
error.body?.error?.details?.push(...details.list);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hono/handler.ts"],"sourcesContent":["import type { Context } from 'hono';\nimport type { HTTPResponseError, Bindings } from 'hono/types';\nimport type { ContentfulStatusCode } from 'hono/utils/http-status';\nimport type { RequestIdVariables } from 'hono/request-id';\nimport { Status, StatusError } from '../status';\nimport { Details } from '../detail';\n\ntype Env = {\n Variables: RequestIdVariables;\n Bindings?: Bindings;\n};\n\nexport function errorHandler<E extends Env = any>(\n error: Error | HTTPResponseError,\n c: Context<E>\n): Response | Promise<Response> {\n const requestId = c.get('requestId');\n const servingData = `${c.req.method}: ${c.req.
|
|
1
|
+
{"version":3,"sources":["../../src/hono/handler.ts"],"sourcesContent":["import type { Context } from 'hono';\nimport type { HTTPResponseError, Bindings } from 'hono/types';\nimport type { ContentfulStatusCode } from 'hono/utils/http-status';\nimport type { RequestIdVariables } from 'hono/request-id';\nimport { Status, StatusError } from '../status';\nimport { Details } from '../detail';\n\ntype Env = {\n Variables: RequestIdVariables;\n Bindings?: Bindings;\n};\n\nexport function errorHandler<E extends Env = any>(\n error: Error | HTTPResponseError,\n c: Context<E>\n): Response | Promise<Response> {\n const requestId = c.get('requestId');\n const servingData = `${c.req.method}: ${c.req.path}`;\n const details = Details.new().requestInfo({ requestId, servingData });\n\n if (error instanceof StatusError) {\n error.body?.error?.details?.push(...details.list);\n return c.json(error.body, error.status as ContentfulStatusCode);\n }\n\n console.error(error);\n return Status.internal('Unknown error').response(details);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,oBAAoC;AACpC,oBAAwB;AAOjB,SAAS,aACd,OACA,GAC8B;AAC9B,QAAM,YAAY,EAAE,IAAI,WAAW;AACnC,QAAM,cAAc,GAAG,EAAE,IAAI,MAAM,KAAK,EAAE,IAAI,IAAI;AAClD,QAAM,UAAU,sBAAQ,IAAI,EAAE,YAAY,EAAE,WAAW,YAAY,CAAC;AAEpE,MAAI,iBAAiB,2BAAa;AAChC,UAAM,MAAM,OAAO,SAAS,KAAK,GAAG,QAAQ,IAAI;AAChD,WAAO,EAAE,KAAK,MAAM,MAAM,MAAM,MAA8B;AAAA,EAChE;AAEA,UAAQ,MAAM,KAAK;AACnB,SAAO,qBAAO,SAAS,eAAe,EAAE,SAAS,OAAO;AAC1D;","names":[]}
|
package/dist/hono/handler.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import { Status, StatusError } from "../status.mjs";
|
|
|
3
3
|
import { Details } from "../detail.mjs";
|
|
4
4
|
function errorHandler(error, c) {
|
|
5
5
|
const requestId = c.get("requestId");
|
|
6
|
-
const servingData = `${c.req.method}: ${c.req.
|
|
6
|
+
const servingData = `${c.req.method}: ${c.req.path}`;
|
|
7
7
|
const details = Details.new().requestInfo({ requestId, servingData });
|
|
8
8
|
if (error instanceof StatusError) {
|
|
9
9
|
error.body?.error?.details?.push(...details.list);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hono/handler.ts"],"sourcesContent":["import type { Context } from 'hono';\nimport type { HTTPResponseError, Bindings } from 'hono/types';\nimport type { ContentfulStatusCode } from 'hono/utils/http-status';\nimport type { RequestIdVariables } from 'hono/request-id';\nimport { Status, StatusError } from '../status';\nimport { Details } from '../detail';\n\ntype Env = {\n Variables: RequestIdVariables;\n Bindings?: Bindings;\n};\n\nexport function errorHandler<E extends Env = any>(\n error: Error | HTTPResponseError,\n c: Context<E>\n): Response | Promise<Response> {\n const requestId = c.get('requestId');\n const servingData = `${c.req.method}: ${c.req.
|
|
1
|
+
{"version":3,"sources":["../../src/hono/handler.ts"],"sourcesContent":["import type { Context } from 'hono';\nimport type { HTTPResponseError, Bindings } from 'hono/types';\nimport type { ContentfulStatusCode } from 'hono/utils/http-status';\nimport type { RequestIdVariables } from 'hono/request-id';\nimport { Status, StatusError } from '../status';\nimport { Details } from '../detail';\n\ntype Env = {\n Variables: RequestIdVariables;\n Bindings?: Bindings;\n};\n\nexport function errorHandler<E extends Env = any>(\n error: Error | HTTPResponseError,\n c: Context<E>\n): Response | Promise<Response> {\n const requestId = c.get('requestId');\n const servingData = `${c.req.method}: ${c.req.path}`;\n const details = Details.new().requestInfo({ requestId, servingData });\n\n if (error instanceof StatusError) {\n error.body?.error?.details?.push(...details.list);\n return c.json(error.body, error.status as ContentfulStatusCode);\n }\n\n console.error(error);\n return Status.internal('Unknown error').response(details);\n}\n"],"mappings":";AAIA,SAAS,QAAQ,mBAAmB;AACpC,SAAS,eAAe;AAOjB,SAAS,aACd,OACA,GAC8B;AAC9B,QAAM,YAAY,EAAE,IAAI,WAAW;AACnC,QAAM,cAAc,GAAG,EAAE,IAAI,MAAM,KAAK,EAAE,IAAI,IAAI;AAClD,QAAM,UAAU,QAAQ,IAAI,EAAE,YAAY,EAAE,WAAW,YAAY,CAAC;AAEpE,MAAI,iBAAiB,aAAa;AAChC,UAAM,MAAM,OAAO,SAAS,KAAK,GAAG,QAAQ,IAAI;AAChD,WAAO,EAAE,KAAK,MAAM,MAAM,MAAM,MAA8B;AAAA,EAChE;AAEA,UAAQ,MAAM,KAAK;AACnB,SAAO,OAAO,SAAS,eAAe,EAAE,SAAS,OAAO;AAC1D;","names":[]}
|
package/dist/index.cjs
CHANGED
|
@@ -22,12 +22,14 @@ __reExport(index_exports, require("./detail.cjs"), module.exports);
|
|
|
22
22
|
__reExport(index_exports, require("./status.cjs"), module.exports);
|
|
23
23
|
__reExport(index_exports, require("./vaild.cjs"), module.exports);
|
|
24
24
|
__reExport(index_exports, require("./snowflake.cjs"), module.exports);
|
|
25
|
+
__reExport(index_exports, require("./response.cjs"), module.exports);
|
|
25
26
|
// Annotate the CommonJS export names for ESM import in node:
|
|
26
27
|
0 && (module.exports = {
|
|
27
28
|
...require("./reason.cjs"),
|
|
28
29
|
...require("./detail.cjs"),
|
|
29
30
|
...require("./status.cjs"),
|
|
30
31
|
...require("./vaild.cjs"),
|
|
31
|
-
...require("./snowflake.cjs")
|
|
32
|
+
...require("./snowflake.cjs"),
|
|
33
|
+
...require("./response.cjs")
|
|
32
34
|
});
|
|
33
35
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @example\n * import { Details, Status } from '@repo/error';\n *\n * Status.adapter = () => new Error('Error');\n *\n * const details = Details.new()\n * .requestInfo({ requestId: '1234567890', servingData: '/v1/tests' })\n * .errorInfo({ reason: 'ACCOUNT_LOCKED' });\n *\n * throw Status.alreadyExists('xxx').error(details);\n */\n\nexport * from './reason';\nexport * from './detail';\nexport * from './status';\nexport * from './vaild';\nexport * from './snowflake';\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;AAAA;AAaA,0BAAc,yBAbd;AAcA,0BAAc,yBAdd;AAeA,0BAAc,yBAfd;AAgBA,0BAAc,wBAhBd;AAiBA,0BAAc,4BAjBd;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @example\n * import { Details, Status } from '@repo/error';\n *\n * Status.adapter = () => new Error('Error');\n *\n * const details = Details.new()\n * .requestInfo({ requestId: '1234567890', servingData: '/v1/tests' })\n * .errorInfo({ reason: 'ACCOUNT_LOCKED' });\n *\n * throw Status.alreadyExists('xxx').error(details);\n */\n\nexport * from './reason';\nexport * from './detail';\nexport * from './status';\nexport * from './vaild';\nexport * from './snowflake';\nexport * from './response';\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;AAAA;AAaA,0BAAc,yBAbd;AAcA,0BAAc,yBAdd;AAeA,0BAAc,yBAfd;AAgBA,0BAAc,wBAhBd;AAiBA,0BAAc,4BAjBd;AAkBA,0BAAc,2BAlBd;","names":[]}
|
package/dist/index.d.cts
CHANGED
|
@@ -3,4 +3,5 @@ export { BadRequest, DebugInfo, Detail, DetailType, Details, ErrorInfo, Help, Lo
|
|
|
3
3
|
export { Code, DEFAULT_MESSAGES, ErrorBody, Status, StatusCode, StatusError } from './status.cjs';
|
|
4
4
|
export { Result, valid } from './vaild.cjs';
|
|
5
5
|
export { UidGenerator, uid } from './snowflake.cjs';
|
|
6
|
+
export { Cursor, Empty, Entity, EntityId, InitParams, NextParams, PageParams, PagedResponse, ParentPageParams, PrevParams, Response, pageParamsSchema } from './response.cjs';
|
|
6
7
|
import 'zod';
|
package/dist/index.d.ts
CHANGED
|
@@ -3,4 +3,5 @@ export { BadRequest, DebugInfo, Detail, DetailType, Details, ErrorInfo, Help, Lo
|
|
|
3
3
|
export { Code, DEFAULT_MESSAGES, ErrorBody, Status, StatusCode, StatusError } from './status.js';
|
|
4
4
|
export { Result, valid } from './vaild.js';
|
|
5
5
|
export { UidGenerator, uid } from './snowflake.js';
|
|
6
|
+
export { Cursor, Empty, Entity, EntityId, InitParams, NextParams, PageParams, PagedResponse, ParentPageParams, PrevParams, Response, pageParamsSchema } from './response.js';
|
|
6
7
|
import 'zod';
|
package/dist/index.mjs
CHANGED
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @example\n * import { Details, Status } from '@repo/error';\n *\n * Status.adapter = () => new Error('Error');\n *\n * const details = Details.new()\n * .requestInfo({ requestId: '1234567890', servingData: '/v1/tests' })\n * .errorInfo({ reason: 'ACCOUNT_LOCKED' });\n *\n * throw Status.alreadyExists('xxx').error(details);\n */\n\nexport * from './reason';\nexport * from './detail';\nexport * from './status';\nexport * from './vaild';\nexport * from './snowflake';\n"],"mappings":";AAaA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @example\n * import { Details, Status } from '@repo/error';\n *\n * Status.adapter = () => new Error('Error');\n *\n * const details = Details.new()\n * .requestInfo({ requestId: '1234567890', servingData: '/v1/tests' })\n * .errorInfo({ reason: 'ACCOUNT_LOCKED' });\n *\n * throw Status.alreadyExists('xxx').error(details);\n */\n\nexport * from './reason';\nexport * from './detail';\nexport * from './status';\nexport * from './vaild';\nexport * from './snowflake';\nexport * from './response';\n"],"mappings":";AAaA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/response.ts
|
|
21
|
+
var response_exports = {};
|
|
22
|
+
__export(response_exports, {
|
|
23
|
+
Cursor: () => Cursor,
|
|
24
|
+
pageParamsSchema: () => pageParamsSchema
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(response_exports);
|
|
27
|
+
var import_zod = require("zod");
|
|
28
|
+
function pageParamsSchema(max = 100, defaultLimit = 20) {
|
|
29
|
+
return import_zod.z.object({
|
|
30
|
+
limit: import_zod.z.coerce.number().int().min(1).max(max).default(defaultLimit),
|
|
31
|
+
prev: import_zod.z.string().optional(),
|
|
32
|
+
next: import_zod.z.string().optional()
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
var Cursor = {
|
|
36
|
+
of(prev, next) {
|
|
37
|
+
return { prev: this.encode(prev), next: this.encode(next) };
|
|
38
|
+
},
|
|
39
|
+
empty() {
|
|
40
|
+
return { prev: "", next: "" };
|
|
41
|
+
},
|
|
42
|
+
encode(id) {
|
|
43
|
+
return Buffer.from(id.toString(), "utf-8").toString("base64");
|
|
44
|
+
},
|
|
45
|
+
decode(token, type = "bigint") {
|
|
46
|
+
const value = Buffer.from(token, "base64").toString("utf-8");
|
|
47
|
+
if (type === "bigint") return BigInt(value);
|
|
48
|
+
if (type === "number") return Number(value);
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
53
|
+
0 && (module.exports = {
|
|
54
|
+
Cursor,
|
|
55
|
+
pageParamsSchema
|
|
56
|
+
});
|
|
57
|
+
//# sourceMappingURL=response.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/response.ts"],"sourcesContent":["import { z } from 'zod';\n\nexport type Empty = {};\nexport type EntityId = string | number;\nexport type Entity = { id: EntityId };\n\nexport type Response<T = never> = { data: T };\n\nexport type InitParams = { limit: number; parent?: string };\nexport type NextParams = { limit: number; parent?: string; next: string };\nexport type PrevParams = { limit: number; parent?: string; prev: string };\nexport type PageParams = { limit: number; prev?: string; next?: string };\nexport type ParentPageParams = { limit: number; parent: string; prev?: string; next?: string };\n\nexport type PagedResponse<T = never> = { data: T[]; paging: { next: string; prev: string } };\n\nexport function pageParamsSchema(max: number = 100, defaultLimit: number = 20) {\n return z.object({\n limit: z.coerce.number().int().min(1).max(max).default(defaultLimit),\n prev: z.string().optional(),\n next: z.string().optional(),\n });\n}\n\nexport const Cursor = {\n of(prev: bigint | number | string, next: bigint | number | string) {\n return { prev: this.encode(prev), next: this.encode(next) };\n },\n empty() {\n return { prev: '', next: '' };\n },\n encode(id: bigint | number | string): string {\n return Buffer.from(id.toString(), 'utf-8').toString('base64');\n },\n decode<T extends 'bigint' | 'number' | 'string' = 'bigint'>(\n token: string,\n type: T = 'bigint' as T\n ): T extends 'bigint' ? bigint : T extends 'number' ? number : string {\n const value = Buffer.from(token, 'base64').toString('utf-8');\n if (type === 'bigint') return BigInt(value) as any;\n if (type === 'number') return Number(value) as any;\n return value as any;\n },\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,iBAAkB;AAgBX,SAAS,iBAAiB,MAAc,KAAK,eAAuB,IAAI;AAC7E,SAAO,aAAE,OAAO;AAAA,IACd,OAAO,aAAE,OAAO,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG,EAAE,QAAQ,YAAY;AAAA,IACnE,MAAM,aAAE,OAAO,EAAE,SAAS;AAAA,IAC1B,MAAM,aAAE,OAAO,EAAE,SAAS;AAAA,EAC5B,CAAC;AACH;AAEO,IAAM,SAAS;AAAA,EACpB,GAAG,MAAgC,MAAgC;AACjE,WAAO,EAAE,MAAM,KAAK,OAAO,IAAI,GAAG,MAAM,KAAK,OAAO,IAAI,EAAE;AAAA,EAC5D;AAAA,EACA,QAAQ;AACN,WAAO,EAAE,MAAM,IAAI,MAAM,GAAG;AAAA,EAC9B;AAAA,EACA,OAAO,IAAsC;AAC3C,WAAO,OAAO,KAAK,GAAG,SAAS,GAAG,OAAO,EAAE,SAAS,QAAQ;AAAA,EAC9D;AAAA,EACA,OACE,OACA,OAAU,UAC0D;AACpE,UAAM,QAAQ,OAAO,KAAK,OAAO,QAAQ,EAAE,SAAS,OAAO;AAC3D,QAAI,SAAS,SAAU,QAAO,OAAO,KAAK;AAC1C,QAAI,SAAS,SAAU,QAAO,OAAO,KAAK;AAC1C,WAAO;AAAA,EACT;AACF;","names":[]}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
type Empty = {};
|
|
4
|
+
type EntityId = string | number;
|
|
5
|
+
type Entity = {
|
|
6
|
+
id: EntityId;
|
|
7
|
+
};
|
|
8
|
+
type Response<T = never> = {
|
|
9
|
+
data: T;
|
|
10
|
+
};
|
|
11
|
+
type InitParams = {
|
|
12
|
+
limit: number;
|
|
13
|
+
parent?: string;
|
|
14
|
+
};
|
|
15
|
+
type NextParams = {
|
|
16
|
+
limit: number;
|
|
17
|
+
parent?: string;
|
|
18
|
+
next: string;
|
|
19
|
+
};
|
|
20
|
+
type PrevParams = {
|
|
21
|
+
limit: number;
|
|
22
|
+
parent?: string;
|
|
23
|
+
prev: string;
|
|
24
|
+
};
|
|
25
|
+
type PageParams = {
|
|
26
|
+
limit: number;
|
|
27
|
+
prev?: string;
|
|
28
|
+
next?: string;
|
|
29
|
+
};
|
|
30
|
+
type ParentPageParams = {
|
|
31
|
+
limit: number;
|
|
32
|
+
parent: string;
|
|
33
|
+
prev?: string;
|
|
34
|
+
next?: string;
|
|
35
|
+
};
|
|
36
|
+
type PagedResponse<T = never> = {
|
|
37
|
+
data: T[];
|
|
38
|
+
paging: {
|
|
39
|
+
next: string;
|
|
40
|
+
prev: string;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
declare function pageParamsSchema(max?: number, defaultLimit?: number): z.ZodObject<{
|
|
44
|
+
limit: z.ZodDefault<z.ZodNumber>;
|
|
45
|
+
prev: z.ZodOptional<z.ZodString>;
|
|
46
|
+
next: z.ZodOptional<z.ZodString>;
|
|
47
|
+
}, "strip", z.ZodTypeAny, {
|
|
48
|
+
limit: number;
|
|
49
|
+
prev?: string | undefined;
|
|
50
|
+
next?: string | undefined;
|
|
51
|
+
}, {
|
|
52
|
+
limit?: number | undefined;
|
|
53
|
+
prev?: string | undefined;
|
|
54
|
+
next?: string | undefined;
|
|
55
|
+
}>;
|
|
56
|
+
declare const Cursor: {
|
|
57
|
+
of(prev: bigint | number | string, next: bigint | number | string): {
|
|
58
|
+
prev: string;
|
|
59
|
+
next: string;
|
|
60
|
+
};
|
|
61
|
+
empty(): {
|
|
62
|
+
prev: string;
|
|
63
|
+
next: string;
|
|
64
|
+
};
|
|
65
|
+
encode(id: bigint | number | string): string;
|
|
66
|
+
decode<T extends "bigint" | "number" | "string" = "bigint">(token: string, type?: T): T extends "bigint" ? bigint : T extends "number" ? number : string;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export { Cursor, type Empty, type Entity, type EntityId, type InitParams, type NextParams, type PageParams, type PagedResponse, type ParentPageParams, type PrevParams, type Response, pageParamsSchema };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
type Empty = {};
|
|
4
|
+
type EntityId = string | number;
|
|
5
|
+
type Entity = {
|
|
6
|
+
id: EntityId;
|
|
7
|
+
};
|
|
8
|
+
type Response<T = never> = {
|
|
9
|
+
data: T;
|
|
10
|
+
};
|
|
11
|
+
type InitParams = {
|
|
12
|
+
limit: number;
|
|
13
|
+
parent?: string;
|
|
14
|
+
};
|
|
15
|
+
type NextParams = {
|
|
16
|
+
limit: number;
|
|
17
|
+
parent?: string;
|
|
18
|
+
next: string;
|
|
19
|
+
};
|
|
20
|
+
type PrevParams = {
|
|
21
|
+
limit: number;
|
|
22
|
+
parent?: string;
|
|
23
|
+
prev: string;
|
|
24
|
+
};
|
|
25
|
+
type PageParams = {
|
|
26
|
+
limit: number;
|
|
27
|
+
prev?: string;
|
|
28
|
+
next?: string;
|
|
29
|
+
};
|
|
30
|
+
type ParentPageParams = {
|
|
31
|
+
limit: number;
|
|
32
|
+
parent: string;
|
|
33
|
+
prev?: string;
|
|
34
|
+
next?: string;
|
|
35
|
+
};
|
|
36
|
+
type PagedResponse<T = never> = {
|
|
37
|
+
data: T[];
|
|
38
|
+
paging: {
|
|
39
|
+
next: string;
|
|
40
|
+
prev: string;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
declare function pageParamsSchema(max?: number, defaultLimit?: number): z.ZodObject<{
|
|
44
|
+
limit: z.ZodDefault<z.ZodNumber>;
|
|
45
|
+
prev: z.ZodOptional<z.ZodString>;
|
|
46
|
+
next: z.ZodOptional<z.ZodString>;
|
|
47
|
+
}, "strip", z.ZodTypeAny, {
|
|
48
|
+
limit: number;
|
|
49
|
+
prev?: string | undefined;
|
|
50
|
+
next?: string | undefined;
|
|
51
|
+
}, {
|
|
52
|
+
limit?: number | undefined;
|
|
53
|
+
prev?: string | undefined;
|
|
54
|
+
next?: string | undefined;
|
|
55
|
+
}>;
|
|
56
|
+
declare const Cursor: {
|
|
57
|
+
of(prev: bigint | number | string, next: bigint | number | string): {
|
|
58
|
+
prev: string;
|
|
59
|
+
next: string;
|
|
60
|
+
};
|
|
61
|
+
empty(): {
|
|
62
|
+
prev: string;
|
|
63
|
+
next: string;
|
|
64
|
+
};
|
|
65
|
+
encode(id: bigint | number | string): string;
|
|
66
|
+
decode<T extends "bigint" | "number" | "string" = "bigint">(token: string, type?: T): T extends "bigint" ? bigint : T extends "number" ? number : string;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export { Cursor, type Empty, type Entity, type EntityId, type InitParams, type NextParams, type PageParams, type PagedResponse, type ParentPageParams, type PrevParams, type Response, pageParamsSchema };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// src/response.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
function pageParamsSchema(max = 100, defaultLimit = 20) {
|
|
4
|
+
return z.object({
|
|
5
|
+
limit: z.coerce.number().int().min(1).max(max).default(defaultLimit),
|
|
6
|
+
prev: z.string().optional(),
|
|
7
|
+
next: z.string().optional()
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
var Cursor = {
|
|
11
|
+
of(prev, next) {
|
|
12
|
+
return { prev: this.encode(prev), next: this.encode(next) };
|
|
13
|
+
},
|
|
14
|
+
empty() {
|
|
15
|
+
return { prev: "", next: "" };
|
|
16
|
+
},
|
|
17
|
+
encode(id) {
|
|
18
|
+
return Buffer.from(id.toString(), "utf-8").toString("base64");
|
|
19
|
+
},
|
|
20
|
+
decode(token, type = "bigint") {
|
|
21
|
+
const value = Buffer.from(token, "base64").toString("utf-8");
|
|
22
|
+
if (type === "bigint") return BigInt(value);
|
|
23
|
+
if (type === "number") return Number(value);
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
export {
|
|
28
|
+
Cursor,
|
|
29
|
+
pageParamsSchema
|
|
30
|
+
};
|
|
31
|
+
//# sourceMappingURL=response.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/response.ts"],"sourcesContent":["import { z } from 'zod';\n\nexport type Empty = {};\nexport type EntityId = string | number;\nexport type Entity = { id: EntityId };\n\nexport type Response<T = never> = { data: T };\n\nexport type InitParams = { limit: number; parent?: string };\nexport type NextParams = { limit: number; parent?: string; next: string };\nexport type PrevParams = { limit: number; parent?: string; prev: string };\nexport type PageParams = { limit: number; prev?: string; next?: string };\nexport type ParentPageParams = { limit: number; parent: string; prev?: string; next?: string };\n\nexport type PagedResponse<T = never> = { data: T[]; paging: { next: string; prev: string } };\n\nexport function pageParamsSchema(max: number = 100, defaultLimit: number = 20) {\n return z.object({\n limit: z.coerce.number().int().min(1).max(max).default(defaultLimit),\n prev: z.string().optional(),\n next: z.string().optional(),\n });\n}\n\nexport const Cursor = {\n of(prev: bigint | number | string, next: bigint | number | string) {\n return { prev: this.encode(prev), next: this.encode(next) };\n },\n empty() {\n return { prev: '', next: '' };\n },\n encode(id: bigint | number | string): string {\n return Buffer.from(id.toString(), 'utf-8').toString('base64');\n },\n decode<T extends 'bigint' | 'number' | 'string' = 'bigint'>(\n token: string,\n type: T = 'bigint' as T\n ): T extends 'bigint' ? bigint : T extends 'number' ? number : string {\n const value = Buffer.from(token, 'base64').toString('utf-8');\n if (type === 'bigint') return BigInt(value) as any;\n if (type === 'number') return Number(value) as any;\n return value as any;\n },\n};\n"],"mappings":";AAAA,SAAS,SAAS;AAgBX,SAAS,iBAAiB,MAAc,KAAK,eAAuB,IAAI;AAC7E,SAAO,EAAE,OAAO;AAAA,IACd,OAAO,EAAE,OAAO,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG,EAAE,QAAQ,YAAY;AAAA,IACnE,MAAM,EAAE,OAAO,EAAE,SAAS;AAAA,IAC1B,MAAM,EAAE,OAAO,EAAE,SAAS;AAAA,EAC5B,CAAC;AACH;AAEO,IAAM,SAAS;AAAA,EACpB,GAAG,MAAgC,MAAgC;AACjE,WAAO,EAAE,MAAM,KAAK,OAAO,IAAI,GAAG,MAAM,KAAK,OAAO,IAAI,EAAE;AAAA,EAC5D;AAAA,EACA,QAAQ;AACN,WAAO,EAAE,MAAM,IAAI,MAAM,GAAG;AAAA,EAC9B;AAAA,EACA,OAAO,IAAsC;AAC3C,WAAO,OAAO,KAAK,GAAG,SAAS,GAAG,OAAO,EAAE,SAAS,QAAQ;AAAA,EAC9D;AAAA,EACA,OACE,OACA,OAAU,UAC0D;AACpE,UAAM,QAAQ,OAAO,KAAK,OAAO,QAAQ,EAAE,SAAS,OAAO;AAC3D,QAAI,SAAS,SAAU,QAAO,OAAO,KAAK;AAC1C,QAAI,SAAS,SAAU,QAAO,OAAO,KAAK;AAC1C,WAAO;AAAA,EACT;AACF;","names":[]}
|