stitchkit 0.8.0 → 0.8.1

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/cli.js CHANGED
@@ -4,14 +4,10 @@ import {
4
4
  emitResult,
5
5
  parseCliArgs,
6
6
  pollUntilDone
7
- } from "./index-tr7r1530.js";
7
+ } from "./index-f39j6twc.js";
8
8
  import"./index-0ed3bx43.js";
9
- import"./index-x3fcszf8.js";
10
- import"./index-wjwj5bz2.js";
11
- import"./index-eq29zkrx.js";
12
- import"./index-kzfs85xp.js";
13
- import"./index-809wc1tt.js";
14
- import"./index-37x76zdn.js";
9
+ import"./index-jgpsd7dy.js";
10
+ import"./index-tm7dqzxc.js";
15
11
  export {
16
12
  pollUntilDone,
17
13
  parseCliArgs,
@@ -15,7 +15,6 @@ import {
15
15
  rateLimited,
16
16
  unauthorized
17
17
  } from "../index-eq29zkrx.js";
18
- import"../index-37x76zdn.js";
19
18
  export {
20
19
  unauthorized,
21
20
  rateLimited,
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  getClientInfo,
3
3
  resolveSocketIp
4
- } from "./index-mwmpw6j1.js";
4
+ } from "./index-p9m9c0jw.js";
5
5
 
6
6
  // src/observability/trace.ts
7
7
  var TRACEPARENT_RE = /^00-([0-9a-f]{32})-([0-9a-f]{16})-[0-9a-f]{2}$/i;
@@ -1,14 +1,12 @@
1
1
  import {
2
2
  forbidden,
3
3
  unauthorized
4
- } from "./index-eq29zkrx.js";
4
+ } from "./index-jgpsd7dy.js";
5
5
  import {
6
+ isRecord,
6
7
  isUnsafeKey,
7
8
  safeJsonParse
8
- } from "./index-kzfs85xp.js";
9
- import {
10
- isRecord
11
- } from "./index-809wc1tt.js";
9
+ } from "./index-tm7dqzxc.js";
12
10
 
13
11
  // src/server/middleware/cookies.ts
14
12
  function parseCookies(header) {
@@ -216,4 +214,9 @@ async function verifyPkce(verifier, challenge) {
216
214
  return derived === challenge;
217
215
  }
218
216
 
219
- export { parseCookies, serializeCookie, defineCookie, verifyJwt, signJwt, extractToken, createAuthHook, createBearerResolver, deriveCodeChallenge, verifyPkce };
217
+ // src/internal/http-input.ts
218
+ function inputIsQuery(method) {
219
+ return method === "GET" || method === "DELETE";
220
+ }
221
+
222
+ export { parseCookies, serializeCookie, defineCookie, verifyJwt, signJwt, extractToken, createAuthHook, createBearerResolver, deriveCodeChallenge, verifyPkce, inputIsQuery };
@@ -2,21 +2,17 @@ import {
2
2
  jsonSchemaFields,
3
3
  toJsonSchema
4
4
  } from "./index-0ed3bx43.js";
5
- import {
6
- isWithinDir
7
- } from "./index-x3fcszf8.js";
8
5
  import {
9
6
  formatZodError,
7
+ isWithinDir,
10
8
  normalizeError,
11
9
  validateHandlerOutput
12
- } from "./index-wjwj5bz2.js";
10
+ } from "./index-jgpsd7dy.js";
13
11
  import {
12
+ isRecord,
14
13
  isUnsafeKey,
15
14
  safeJsonParse
16
- } from "./index-kzfs85xp.js";
17
- import {
18
- isRecord
19
- } from "./index-809wc1tt.js";
15
+ } from "./index-tm7dqzxc.js";
20
16
 
21
17
  // src/tools/coerce.ts
22
18
  import { z } from "zod";
@@ -0,0 +1,105 @@
1
+ // src/contract/errors.ts
2
+ class AppError extends Error {
3
+ code;
4
+ status;
5
+ details;
6
+ hint;
7
+ constructor(code, message, status = 500, details, hint) {
8
+ super(message ?? code);
9
+ this.code = code;
10
+ this.status = status;
11
+ this.details = details;
12
+ this.hint = hint;
13
+ this.name = "AppError";
14
+ }
15
+ static is(err) {
16
+ return err instanceof AppError;
17
+ }
18
+ toJSON() {
19
+ return {
20
+ error: {
21
+ code: this.code,
22
+ message: this.message,
23
+ ...this.details && { details: this.details },
24
+ ...this.hint && { hint: this.hint }
25
+ }
26
+ };
27
+ }
28
+ }
29
+ function notFound(message = "Not found") {
30
+ throw new AppError("NOT_FOUND", message, 404);
31
+ }
32
+ function badRequest(message, details) {
33
+ throw new AppError("BAD_REQUEST", message, 400, details);
34
+ }
35
+ function unauthorized(message = "Unauthorized") {
36
+ throw new AppError("UNAUTHORIZED", message, 401);
37
+ }
38
+ function forbidden(message = "Forbidden") {
39
+ throw new AppError("FORBIDDEN", message, 403);
40
+ }
41
+ function conflict(message = "Conflict", details) {
42
+ throw new AppError("CONFLICT", message, 409, details);
43
+ }
44
+ function rateLimited(message = "Too many requests") {
45
+ throw new AppError("RATE_LIMITED", message, 429);
46
+ }
47
+ var STITCH_ERROR_STATUS = {
48
+ BAD_REQUEST: 400,
49
+ UNAUTHORIZED: 401,
50
+ FORBIDDEN: 403,
51
+ NOT_FOUND: 404,
52
+ METHOD_NOT_ALLOWED: 405,
53
+ CONFLICT: 409,
54
+ RATE_LIMITED: 429,
55
+ VALIDATION_ERROR: 400,
56
+ INTERNAL_SERVER_ERROR: 500
57
+ };
58
+ function isStitchErrorCode(code) {
59
+ return code in STITCH_ERROR_STATUS;
60
+ }
61
+ function appError(code, message, details) {
62
+ throw new AppError(code, message, isStitchErrorCode(code) ? STITCH_ERROR_STATUS[code] : 500, details);
63
+ }
64
+ // src/contract/pagination.ts
65
+ import { z } from "zod";
66
+ // src/internal/errors.ts
67
+ import { z as z2 } from "zod";
68
+ function formatZodError(error) {
69
+ const issues = error.issues.slice(0, 5);
70
+ const lines = issues.map((issue) => {
71
+ const path = issue.path.length > 0 ? issue.path.join(".") : "(root)";
72
+ return `${path}: ${issue.message}`;
73
+ });
74
+ const suffix = error.issues.length > 5 ? `
75
+ ...and ${error.issues.length - 5} more issues` : "";
76
+ return lines.join(`
77
+ `) + suffix;
78
+ }
79
+ function normalizeError(err) {
80
+ if (AppError.is(err))
81
+ return err;
82
+ if (err instanceof z2.ZodError) {
83
+ return new AppError("VALIDATION_ERROR", formatZodError(err), 400);
84
+ }
85
+ console.error("[stitchkit] unhandled error:", err);
86
+ return new AppError("INTERNAL_SERVER_ERROR", "Internal server error", 500);
87
+ }
88
+ function validateHandlerOutput(schema, data) {
89
+ const parsed = schema.safeParse(data);
90
+ if (parsed.success)
91
+ return { ok: true, data: parsed.data };
92
+ return {
93
+ ok: false,
94
+ message: `Handler output does not match the contract: ${formatZodError(parsed.error)}`
95
+ };
96
+ }
97
+
98
+ // src/internal/within-dir.ts
99
+ import { sep } from "node:path";
100
+ function isWithinDir(root, target) {
101
+ const base = root.endsWith(sep) ? root.slice(0, -sep.length) : root;
102
+ return target === root || target === base || target.startsWith(base + sep);
103
+ }
104
+
105
+ export { AppError, notFound, badRequest, unauthorized, forbidden, conflict, rateLimited, STITCH_ERROR_STATUS, isStitchErrorCode, appError, formatZodError, normalizeError, validateHandlerOutput, isWithinDir };
@@ -1,9 +1,7 @@
1
1
  import {
2
+ isRecord,
2
3
  isUnsafeKey
3
- } from "./index-kzfs85xp.js";
4
- import {
5
- isRecord
6
- } from "./index-809wc1tt.js";
4
+ } from "./index-tm7dqzxc.js";
7
5
 
8
6
  // src/server/request.ts
9
7
  function generateTraceId() {
@@ -1,31 +1,23 @@
1
1
  import {
2
- isWithinDir
3
- } from "./index-x3fcszf8.js";
4
- import {
2
+ AppError,
3
+ badRequest,
4
+ isWithinDir,
5
5
  normalizeError,
6
6
  validateHandlerOutput
7
- } from "./index-wjwj5bz2.js";
8
- import {
9
- AppError,
10
- badRequest
11
- } from "./index-eq29zkrx.js";
7
+ } from "./index-jgpsd7dy.js";
12
8
  import {
13
9
  extractIp,
14
10
  getClientInfo,
15
11
  parseQueryParams,
16
12
  resolveSocketIp,
17
13
  resolveTraceId
18
- } from "./index-mwmpw6j1.js";
14
+ } from "./index-p9m9c0jw.js";
19
15
  import {
16
+ __require,
20
17
  isUnsafeKey,
21
- safeJsonParse
22
- } from "./index-kzfs85xp.js";
23
- import {
18
+ safeJsonParse,
24
19
  typedEntries
25
- } from "./index-809wc1tt.js";
26
- import {
27
- __require
28
- } from "./index-37x76zdn.js";
20
+ } from "./index-tm7dqzxc.js";
29
21
 
30
22
  // src/server/multipart.ts
31
23
  var DEFAULT_MAX_UPLOAD_BYTES = 25 * 1024 * 1024;
@@ -0,0 +1,20 @@
1
+ import { createRequire } from "node:module";
2
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
3
+
4
+ // src/internal/safe-json.ts
5
+ function isUnsafeKey(key) {
6
+ return key === "__proto__";
7
+ }
8
+ function safeJsonParse(text) {
9
+ return JSON.parse(text, (key, value) => isUnsafeKey(key) ? undefined : value);
10
+ }
11
+
12
+ // src/internal/typed.ts
13
+ function typedEntries(value) {
14
+ return Object.entries(value);
15
+ }
16
+ function isRecord(value) {
17
+ return typeof value === "object" && value !== null && !Array.isArray(value);
18
+ }
19
+
20
+ export { __require, isUnsafeKey, safeJsonParse, typedEntries, isRecord };