sprint-es 0.0.148 → 0.0.150

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.
@@ -31,6 +31,7 @@ const morgan = require("morgan");
31
31
  const dotenv = require("dotenv");
32
32
  const url = require("url");
33
33
  const rateLimit = require("toolkitify/rate-limit");
34
+ const modules_schemas_index = require("./modules/schemas/index.cjs");
34
35
  var _documentCurrentScript = typeof document !== "undefined" ? document.currentScript : null;
35
36
  process.argv.includes("--dev");
36
37
  const isVerbose = process.argv.includes("--verbose");
@@ -829,6 +830,7 @@ class Sprint {
829
830
  }
830
831
  }
831
832
  function createSchemaValidationMiddleware(schema) {
833
+ const headersSchema = schema.headers ? modules_schemas_index.normalizeHeadersSchema(schema.headers) : null;
832
834
  return (req, res, next) => {
833
835
  const errors = [];
834
836
  const method = req.method.toUpperCase();
@@ -863,9 +865,9 @@ function createSchemaValidationMiddleware(schema) {
863
865
  })));
864
866
  }
865
867
  }
866
- if (schema.headers) {
868
+ if (headersSchema) {
867
869
  const normalizedHeaders = Object.fromEntries(Object.entries(req.headers).map(([key, value]) => [key.toLowerCase(), value]));
868
- const result = schema.headers.safeParse(normalizedHeaders);
870
+ const result = headersSchema.safeParse(normalizedHeaders);
869
871
  if (!result.success) {
870
872
  errors.push(...result.error.issues.map((issue) => ({
871
873
  location: "headers",
@@ -1,6 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const zod = require("zod");
4
+ function normalizeHeadersSchema(schema) {
5
+ const shape = schema.shape ?? schema._def?.shape?.();
6
+ if (!shape) return schema;
7
+ const normalizedShape = Object.fromEntries(Object.entries(shape).map(([key, value]) => [key.toLowerCase(), value]));
8
+ return zod.z.object(normalizedShape);
9
+ }
4
10
  function createSprintAuthorizationSchema(options) {
5
11
  const defaultSources = ["query:token", "headers:authorization"];
6
12
  const sources = options?.sources ? Array.isArray(options.sources) ? options.sources : [options.sources] : defaultSources;
@@ -36,6 +42,7 @@ function parseSchema(schema, data) {
36
42
  return { success: true, data: result.data };
37
43
  }
38
44
  function defineRouteSchema(schema) {
45
+ const headersSchema = schema.headers ? normalizeHeadersSchema(schema.headers) : null;
39
46
  const middleware = (req, res, next) => {
40
47
  const errors = [];
41
48
  const method = req.method.toUpperCase();
@@ -52,9 +59,9 @@ function defineRouteSchema(schema) {
52
59
  const result = parseSchema(schema.params, req.params);
53
60
  if (!result.success) errors.push(...result.errors.map((e) => ({ location: "params", ...e })));
54
61
  }
55
- if (schema.headers) {
62
+ if (headersSchema) {
56
63
  const normalizedHeaders = Object.fromEntries(Object.entries(req.headers).map(([key, value]) => [key.toLowerCase(), value]));
57
- const result = parseSchema(schema.headers, normalizedHeaders);
64
+ const result = headersSchema.safeParse(normalizedHeaders);
58
65
  if (!result.success) errors.push(...result.errors.map((e) => ({ location: "headers", ...e })));
59
66
  }
60
67
  if (schema.sprint?.authorization) {
@@ -101,5 +108,6 @@ function defineRouteSchema(schema) {
101
108
  return middleware;
102
109
  }
103
110
  exports.defineRouteSchema = defineRouteSchema;
111
+ exports.normalizeHeadersSchema = normalizeHeadersSchema;
104
112
  exports.sprint = sprintBuilder;
105
113
  exports.z = proxyZ;
package/dist/esm/index.js CHANGED
@@ -7,6 +7,7 @@ import morgan from "morgan";
7
7
  import dotenv from "dotenv";
8
8
  import { pathToFileURL } from "url";
9
9
  import { RateLimiter } from "toolkitify/rate-limit";
10
+ import { normalizeHeadersSchema } from "./modules/schemas/index.js";
10
11
  process.argv.includes("--dev");
11
12
  const isVerbose = process.argv.includes("--verbose");
12
13
  let __filename$1;
@@ -804,6 +805,7 @@ class Sprint {
804
805
  }
805
806
  }
806
807
  function createSchemaValidationMiddleware(schema) {
808
+ const headersSchema = schema.headers ? normalizeHeadersSchema(schema.headers) : null;
807
809
  return (req, res, next) => {
808
810
  const errors = [];
809
811
  const method = req.method.toUpperCase();
@@ -838,9 +840,9 @@ function createSchemaValidationMiddleware(schema) {
838
840
  })));
839
841
  }
840
842
  }
841
- if (schema.headers) {
843
+ if (headersSchema) {
842
844
  const normalizedHeaders = Object.fromEntries(Object.entries(req.headers).map(([key, value]) => [key.toLowerCase(), value]));
843
- const result = schema.headers.safeParse(normalizedHeaders);
845
+ const result = headersSchema.safeParse(normalizedHeaders);
844
846
  if (!result.success) {
845
847
  errors.push(...result.error.issues.map((issue) => ({
846
848
  location: "headers",
@@ -1,4 +1,10 @@
1
1
  import { z } from "zod";
2
+ function normalizeHeadersSchema(schema) {
3
+ const shape = schema.shape ?? schema._def?.shape?.();
4
+ if (!shape) return schema;
5
+ const normalizedShape = Object.fromEntries(Object.entries(shape).map(([key, value]) => [key.toLowerCase(), value]));
6
+ return z.object(normalizedShape);
7
+ }
2
8
  function createSprintAuthorizationSchema(options) {
3
9
  const defaultSources = ["query:token", "headers:authorization"];
4
10
  const sources = options?.sources ? Array.isArray(options.sources) ? options.sources : [options.sources] : defaultSources;
@@ -34,6 +40,7 @@ function parseSchema(schema, data) {
34
40
  return { success: true, data: result.data };
35
41
  }
36
42
  function defineRouteSchema(schema) {
43
+ const headersSchema = schema.headers ? normalizeHeadersSchema(schema.headers) : null;
37
44
  const middleware = (req, res, next) => {
38
45
  const errors = [];
39
46
  const method = req.method.toUpperCase();
@@ -50,9 +57,9 @@ function defineRouteSchema(schema) {
50
57
  const result = parseSchema(schema.params, req.params);
51
58
  if (!result.success) errors.push(...result.errors.map((e) => ({ location: "params", ...e })));
52
59
  }
53
- if (schema.headers) {
60
+ if (headersSchema) {
54
61
  const normalizedHeaders = Object.fromEntries(Object.entries(req.headers).map(([key, value]) => [key.toLowerCase(), value]));
55
- const result = parseSchema(schema.headers, normalizedHeaders);
62
+ const result = headersSchema.safeParse(normalizedHeaders);
56
63
  if (!result.success) errors.push(...result.errors.map((e) => ({ location: "headers", ...e })));
57
64
  }
58
65
  if (schema.sprint?.authorization) {
@@ -100,6 +107,7 @@ function defineRouteSchema(schema) {
100
107
  }
101
108
  export {
102
109
  defineRouteSchema,
110
+ normalizeHeadersSchema,
103
111
  sprintBuilder as sprint,
104
112
  proxyZ as z
105
113
  };
@@ -1 +1 @@
1
- {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../src/middleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAuB,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAkGlF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,SAAS,gBAAgB,EAC7D,MAAM,EAAE,gBAAgB,CAAC,OAAO,CAAC,GAClC,gBAAgB,CAAC,OAAO,CAAC,CAwB3B"}
1
+ {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../src/middleware.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAuB,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAoGlF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,SAAS,gBAAgB,EAC7D,MAAM,EAAE,gBAAgB,CAAC,OAAO,CAAC,GAClC,gBAAgB,CAAC,OAAO,CAAC,CAwB3B"}
@@ -4,6 +4,7 @@ export type AuthorizationSource = `query:${string}` | `headers:${string}`;
4
4
  export interface SprintAuthorizationOptions {
5
5
  sources?: AuthorizationSource | AuthorizationSource[];
6
6
  }
7
+ export declare function normalizeHeadersSchema(schema: any): any;
7
8
  declare function createSprintAuthorizationSchema(options?: SprintAuthorizationOptions): ZodSchemaType<string, ZodTypeDef, string>;
8
9
  declare const sprintBuilder: {
9
10
  authorization: typeof createSprintAuthorizationSchema;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/modules/schemas/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EAAE,CAAC,EAAE,SAAS,IAAI,aAAa,EAAE,UAAU,EAAuB,MAAM,KAAK,CAAC;AAErF,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U,EAAE,GAAG,CAAC,CAAC;IAC7C,OAAO,CAAC,EAAE,aAAa,CAAC,GAAG,EAAE,UAAU,EAAE,GAAG,CAAC,CAAC;IAC9C,MAAM,CAAC,EAAE;QACL,aAAa,CAAC,EAAE,aAAa,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC;KAC7D,CAAC;CACL;AAqBD,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,kBAAkB,EAAE,MAAM,EAAE,CAAC,GAAG,cAAc,CA8EzF;AAED,YAAY,EAAE,SAAS,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sprint-es",
3
- "version": "0.0.148",
3
+ "version": "0.0.150",
4
4
  "description": "Sprint - Quickly API",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",