sst-http 0.7.0 → 1.3.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.
@@ -0,0 +1,13 @@
1
+ // src/paths.ts
2
+ var API_GATEWAY_PARAM_RE = /(^|\/):([A-Za-z0-9_]+(?:[+*])?)(?=\/|$)/g;
3
+ function normalizeRouterPath(path) {
4
+ return path.replace(/\{([^/{}]+)\}/g, ":$1");
5
+ }
6
+ function normalizeApiGatewayPath(path) {
7
+ return path.replace(API_GATEWAY_PARAM_RE, (_match, prefix, name) => `${prefix}{${name}}`);
8
+ }
9
+
10
+ export {
11
+ normalizeRouterPath,
12
+ normalizeApiGatewayPath
13
+ };
package/dist/cli.cjs CHANGED
@@ -6,6 +6,14 @@ var import_node_fs = require("fs");
6
6
  var import_node_path = require("path");
7
7
  var import_node_process = require("process");
8
8
  var import_ts_morph = require("ts-morph");
9
+
10
+ // src/paths.ts
11
+ var API_GATEWAY_PARAM_RE = /(^|\/):([A-Za-z0-9_]+(?:[+*])?)(?=\/|$)/g;
12
+ function normalizeApiGatewayPath(path) {
13
+ return path.replace(API_GATEWAY_PARAM_RE, (_match, prefix, name) => `${prefix}{${name}}`);
14
+ }
15
+
16
+ // src/cli.ts
9
17
  var METHOD_DECORATORS = {
10
18
  Get: "GET",
11
19
  Post: "POST",
@@ -150,7 +158,7 @@ function extractRoute(fn, inferName) {
150
158
  const auth = firebaseDecorator ? readFirebaseAuth(firebaseDecorator) : { type: "none" };
151
159
  return {
152
160
  method,
153
- path,
161
+ path: normalizeApiGatewayPath(path),
154
162
  auth
155
163
  };
156
164
  }
package/dist/cli.js CHANGED
@@ -1,4 +1,7 @@
1
1
  #!/usr/bin/env node
2
+ import {
3
+ normalizeApiGatewayPath
4
+ } from "./chunk-5MOJ3SW6.js";
2
5
 
3
6
  // src/cli.ts
4
7
  import { writeFileSync } from "fs";
@@ -153,7 +156,7 @@ function extractRoute(fn, inferName) {
153
156
  const auth = firebaseDecorator ? readFirebaseAuth(firebaseDecorator) : { type: "none" };
154
157
  return {
155
158
  method,
156
- path,
159
+ path: normalizeApiGatewayPath(path),
157
160
  auth
158
161
  };
159
162
  }
package/dist/index.cjs CHANGED
@@ -26,6 +26,7 @@ __export(index_exports, {
26
26
  FirebaseAuth: () => FirebaseAuth,
27
27
  Get: () => Get,
28
28
  Head: () => Head,
29
+ Header: () => Header,
29
30
  Headers: () => Headers,
30
31
  HttpError: () => HttpError,
31
32
  Options: () => Options,
@@ -121,12 +122,19 @@ function inferPath(handler) {
121
122
 
122
123
  // src/router.ts
123
124
  var import_path_to_regexp = require("path-to-regexp");
125
+
126
+ // src/paths.ts
127
+ function normalizeRouterPath(path) {
128
+ return path.replace(/\{([^/{}]+)\}/g, ":$1");
129
+ }
130
+
131
+ // src/router.ts
124
132
  var Router = class {
125
133
  routes;
126
134
  constructor(entries) {
127
135
  this.routes = entries.map((entry) => ({
128
136
  entry,
129
- matcher: (0, import_path_to_regexp.match)(normalizeRoutePath(entry.path), { decode: decodeURIComponent })
137
+ matcher: (0, import_path_to_regexp.match)(normalizeRouterPath(entry.path), { decode: decodeURIComponent })
130
138
  }));
131
139
  }
132
140
  find(method, pathname) {
@@ -161,9 +169,6 @@ function normalizeParams(params) {
161
169
  }
162
170
  return normalized;
163
171
  }
164
- function normalizeRoutePath(path) {
165
- return path.replace(/\{([^/{}]+)\}/g, ":$1");
166
- }
167
172
 
168
173
  // src/runtime.ts
169
174
  var HTTP_ERROR_MARKER = Symbol.for("sst-http.HttpError");
@@ -301,17 +306,21 @@ function buildHandlerArguments(entry, ctx, getBody) {
301
306
  break;
302
307
  }
303
308
  case "query": {
304
- args[meta.index] = ctx.query;
309
+ args[meta.index] = meta.name ? ctx.query[meta.name] : ctx.query;
305
310
  break;
306
311
  }
307
312
  case "param": {
308
- args[meta.index] = ctx.params;
313
+ args[meta.index] = meta.name ? ctx.params[meta.name] : ctx.params;
309
314
  break;
310
315
  }
311
316
  case "headers": {
312
317
  args[meta.index] = ctx.headers;
313
318
  break;
314
319
  }
320
+ case "header": {
321
+ args[meta.index] = meta.name ? ctx.headers[meta.name.toLowerCase()] : ctx.headers;
322
+ break;
323
+ }
315
324
  case "req": {
316
325
  args[meta.index] = ctx.event;
317
326
  break;
@@ -509,13 +518,14 @@ function createRouteDecorator(method) {
509
518
  registerRoute(handler, method, path);
510
519
  };
511
520
  }
512
- function createParameterDecorator(type, schema) {
521
+ function createParameterDecorator(type, options2) {
513
522
  return (target, propertyKey, parameterIndex) => {
514
523
  const handler = resolveHandler(target, propertyKey);
515
524
  registerParameter(handler, {
516
525
  index: parameterIndex,
517
526
  type,
518
- schema
527
+ schema: options2?.schema,
528
+ name: options2?.name
519
529
  });
520
530
  };
521
531
  }
@@ -536,17 +546,20 @@ function Auth() {
536
546
  return createParameterDecorator("auth");
537
547
  }
538
548
  function Body(schema) {
539
- return createParameterDecorator("body", schema);
549
+ return createParameterDecorator("body", { schema });
540
550
  }
541
- function Query() {
542
- return createParameterDecorator("query");
551
+ function Query(name) {
552
+ return createParameterDecorator("query", { name });
543
553
  }
544
- function Param() {
545
- return createParameterDecorator("param");
554
+ function Param(name) {
555
+ return createParameterDecorator("param", { name });
546
556
  }
547
557
  function Headers() {
548
558
  return createParameterDecorator("headers");
549
559
  }
560
+ function Header(name) {
561
+ return createParameterDecorator("header", { name });
562
+ }
550
563
  function Req() {
551
564
  return createParameterDecorator("req");
552
565
  }
@@ -561,6 +574,7 @@ function Res() {
561
574
  FirebaseAuth,
562
575
  Get,
563
576
  Head,
577
+ Header,
564
578
  Headers,
565
579
  HttpError,
566
580
  Options,
package/dist/index.d.cts CHANGED
@@ -33,12 +33,13 @@ declare const Options: (path?: string) => LegacyDecorator;
33
33
  declare function FirebaseAuth(options?: FirebaseAuthOptions): LegacyDecorator;
34
34
  declare function Auth(): LegacyParameterDecorator;
35
35
  declare function Body(schema?: ZodTypeAny): LegacyParameterDecorator;
36
- declare function Query(): LegacyParameterDecorator;
37
- declare function Param(): LegacyParameterDecorator;
36
+ declare function Query(name?: string): LegacyParameterDecorator;
37
+ declare function Param(name?: string): LegacyParameterDecorator;
38
38
  declare function Headers(): LegacyParameterDecorator;
39
+ declare function Header(name: string): LegacyParameterDecorator;
39
40
  declare function Req(): LegacyParameterDecorator;
40
41
  declare function Res(): LegacyParameterDecorator;
41
42
 
42
43
  declare function configureRoutes(next?: RouteOptions): void;
43
44
 
44
- export { Auth, Body, Delete, FirebaseAuth, FirebaseAuthOptions, Get, Head, Headers, HttpError, Options, Param, Patch, Post, Put, Query, Req, Res, ResponseLike, RouteOptions, configureRoutes, createHandler, handleError, json, noContent, text };
45
+ export { Auth, Body, Delete, FirebaseAuth, FirebaseAuthOptions, Get, Head, Header, Headers, HttpError, Options, Param, Patch, Post, Put, Query, Req, Res, ResponseLike, RouteOptions, configureRoutes, createHandler, handleError, json, noContent, text };
package/dist/index.d.ts CHANGED
@@ -33,12 +33,13 @@ declare const Options: (path?: string) => LegacyDecorator;
33
33
  declare function FirebaseAuth(options?: FirebaseAuthOptions): LegacyDecorator;
34
34
  declare function Auth(): LegacyParameterDecorator;
35
35
  declare function Body(schema?: ZodTypeAny): LegacyParameterDecorator;
36
- declare function Query(): LegacyParameterDecorator;
37
- declare function Param(): LegacyParameterDecorator;
36
+ declare function Query(name?: string): LegacyParameterDecorator;
37
+ declare function Param(name?: string): LegacyParameterDecorator;
38
38
  declare function Headers(): LegacyParameterDecorator;
39
+ declare function Header(name: string): LegacyParameterDecorator;
39
40
  declare function Req(): LegacyParameterDecorator;
40
41
  declare function Res(): LegacyParameterDecorator;
41
42
 
42
43
  declare function configureRoutes(next?: RouteOptions): void;
43
44
 
44
- export { Auth, Body, Delete, FirebaseAuth, FirebaseAuthOptions, Get, Head, Headers, HttpError, Options, Param, Patch, Post, Put, Query, Req, Res, ResponseLike, RouteOptions, configureRoutes, createHandler, handleError, json, noContent, text };
45
+ export { Auth, Body, Delete, FirebaseAuth, FirebaseAuthOptions, Get, Head, Header, Headers, HttpError, Options, Param, Patch, Post, Put, Query, Req, Res, ResponseLike, RouteOptions, configureRoutes, createHandler, handleError, json, noContent, text };
package/dist/index.js CHANGED
@@ -1,3 +1,7 @@
1
+ import {
2
+ normalizeRouterPath
3
+ } from "./chunk-5MOJ3SW6.js";
4
+
1
5
  // src/registry.ts
2
6
  var routeMeta = /* @__PURE__ */ new Map();
3
7
  var parameterMeta = /* @__PURE__ */ new Map();
@@ -79,7 +83,7 @@ var Router = class {
79
83
  constructor(entries) {
80
84
  this.routes = entries.map((entry) => ({
81
85
  entry,
82
- matcher: match(normalizeRoutePath(entry.path), { decode: decodeURIComponent })
86
+ matcher: match(normalizeRouterPath(entry.path), { decode: decodeURIComponent })
83
87
  }));
84
88
  }
85
89
  find(method, pathname) {
@@ -114,9 +118,6 @@ function normalizeParams(params) {
114
118
  }
115
119
  return normalized;
116
120
  }
117
- function normalizeRoutePath(path) {
118
- return path.replace(/\{([^/{}]+)\}/g, ":$1");
119
- }
120
121
 
121
122
  // src/runtime.ts
122
123
  var HTTP_ERROR_MARKER = Symbol.for("sst-http.HttpError");
@@ -254,17 +255,21 @@ function buildHandlerArguments(entry, ctx, getBody) {
254
255
  break;
255
256
  }
256
257
  case "query": {
257
- args[meta.index] = ctx.query;
258
+ args[meta.index] = meta.name ? ctx.query[meta.name] : ctx.query;
258
259
  break;
259
260
  }
260
261
  case "param": {
261
- args[meta.index] = ctx.params;
262
+ args[meta.index] = meta.name ? ctx.params[meta.name] : ctx.params;
262
263
  break;
263
264
  }
264
265
  case "headers": {
265
266
  args[meta.index] = ctx.headers;
266
267
  break;
267
268
  }
269
+ case "header": {
270
+ args[meta.index] = meta.name ? ctx.headers[meta.name.toLowerCase()] : ctx.headers;
271
+ break;
272
+ }
268
273
  case "req": {
269
274
  args[meta.index] = ctx.event;
270
275
  break;
@@ -462,13 +467,14 @@ function createRouteDecorator(method) {
462
467
  registerRoute(handler, method, path);
463
468
  };
464
469
  }
465
- function createParameterDecorator(type, schema) {
470
+ function createParameterDecorator(type, options2) {
466
471
  return (target, propertyKey, parameterIndex) => {
467
472
  const handler = resolveHandler(target, propertyKey);
468
473
  registerParameter(handler, {
469
474
  index: parameterIndex,
470
475
  type,
471
- schema
476
+ schema: options2?.schema,
477
+ name: options2?.name
472
478
  });
473
479
  };
474
480
  }
@@ -489,17 +495,20 @@ function Auth() {
489
495
  return createParameterDecorator("auth");
490
496
  }
491
497
  function Body(schema) {
492
- return createParameterDecorator("body", schema);
498
+ return createParameterDecorator("body", { schema });
493
499
  }
494
- function Query() {
495
- return createParameterDecorator("query");
500
+ function Query(name) {
501
+ return createParameterDecorator("query", { name });
496
502
  }
497
- function Param() {
498
- return createParameterDecorator("param");
503
+ function Param(name) {
504
+ return createParameterDecorator("param", { name });
499
505
  }
500
506
  function Headers() {
501
507
  return createParameterDecorator("headers");
502
508
  }
509
+ function Header(name) {
510
+ return createParameterDecorator("header", { name });
511
+ }
503
512
  function Req() {
504
513
  return createParameterDecorator("req");
505
514
  }
@@ -513,6 +522,7 @@ export {
513
522
  FirebaseAuth,
514
523
  Get,
515
524
  Head,
525
+ Header,
516
526
  Headers,
517
527
  HttpError,
518
528
  Options,
package/dist/infra.cjs CHANGED
@@ -28,6 +28,14 @@ __export(infra_exports, {
28
28
  module.exports = __toCommonJS(infra_exports);
29
29
  var import_node_fs = require("fs");
30
30
  var import_node_path = require("path");
31
+
32
+ // src/paths.ts
33
+ var API_GATEWAY_PARAM_RE = /(^|\/):([A-Za-z0-9_]+(?:[+*])?)(?=\/|$)/g;
34
+ function normalizeApiGatewayPath(path) {
35
+ return path.replace(API_GATEWAY_PARAM_RE, (_match, prefix, name) => `${prefix}{${name}}`);
36
+ }
37
+
38
+ // src/infra.ts
31
39
  function ensureSstAws(source) {
32
40
  if (source?.sst?.aws) {
33
41
  return source.sst.aws;
@@ -58,7 +66,8 @@ function wireApiFromManifest(manifest, opts) {
58
66
  }
59
67
  for (const route of manifest.routes) {
60
68
  const isProtected = route.auth.type === "firebase";
61
- const path = route.path.startsWith("/") ? route.path : `/${route.path}`;
69
+ const rawPath = route.path.startsWith("/") ? route.path : `/${route.path}`;
70
+ const path = normalizeApiGatewayPath(rawPath);
62
71
  const authConfig = isProtected && route.auth.type === "firebase" ? {
63
72
  name: "firebase",
64
73
  optional: route.auth.optional,
@@ -124,7 +133,8 @@ function httpApiAdapter(args) {
124
133
  };
125
134
  const registerRoute = (method, path, config) => {
126
135
  const apiAny = api;
127
- const routeKey = `${method} ${path}`;
136
+ const normalizedPath = normalizeApiGatewayPath(path);
137
+ const routeKey = `${method} ${normalizedPath}`;
128
138
  const asAny = config.handler;
129
139
  const handlerInput = typeof asAny === "string" ? asAny : asAny && typeof asAny.arn !== "undefined" ? asAny.arn : asAny && typeof asAny.handler === "string" ? asAny.handler : asAny === void 0 ? void 0 : (() => {
130
140
  throw new Error("Unsupported handler type: provide a handler string, FunctionArgs, or a Function ARN/output");
@@ -197,7 +207,8 @@ function restApiAdapter(args) {
197
207
  };
198
208
  const registerRoute = (method, path, config) => {
199
209
  const apiAny = api;
200
- const routeKey = `${method} ${path}`;
210
+ const normalizedPath = normalizeApiGatewayPath(path);
211
+ const routeKey = `${method} ${normalizedPath}`;
201
212
  const asAny = config.handler;
202
213
  const handlerInput = typeof asAny === "string" ? asAny : asAny && typeof asAny.arn !== "undefined" ? asAny.arn : asAny && typeof asAny.handler === "string" ? asAny.handler : asAny === void 0 ? void 0 : (() => {
203
214
  throw new Error("Unsupported handler type: provide a handler string, FunctionArgs, or a Function ARN/output");
package/dist/infra.js CHANGED
@@ -1,3 +1,7 @@
1
+ import {
2
+ normalizeApiGatewayPath
3
+ } from "./chunk-5MOJ3SW6.js";
4
+
1
5
  // src/infra.ts
2
6
  import { readFileSync } from "fs";
3
7
  import { resolve } from "path";
@@ -31,7 +35,8 @@ function wireApiFromManifest(manifest, opts) {
31
35
  }
32
36
  for (const route of manifest.routes) {
33
37
  const isProtected = route.auth.type === "firebase";
34
- const path = route.path.startsWith("/") ? route.path : `/${route.path}`;
38
+ const rawPath = route.path.startsWith("/") ? route.path : `/${route.path}`;
39
+ const path = normalizeApiGatewayPath(rawPath);
35
40
  const authConfig = isProtected && route.auth.type === "firebase" ? {
36
41
  name: "firebase",
37
42
  optional: route.auth.optional,
@@ -97,7 +102,8 @@ function httpApiAdapter(args) {
97
102
  };
98
103
  const registerRoute = (method, path, config) => {
99
104
  const apiAny = api;
100
- const routeKey = `${method} ${path}`;
105
+ const normalizedPath = normalizeApiGatewayPath(path);
106
+ const routeKey = `${method} ${normalizedPath}`;
101
107
  const asAny = config.handler;
102
108
  const handlerInput = typeof asAny === "string" ? asAny : asAny && typeof asAny.arn !== "undefined" ? asAny.arn : asAny && typeof asAny.handler === "string" ? asAny.handler : asAny === void 0 ? void 0 : (() => {
103
109
  throw new Error("Unsupported handler type: provide a handler string, FunctionArgs, or a Function ARN/output");
@@ -170,7 +176,8 @@ function restApiAdapter(args) {
170
176
  };
171
177
  const registerRoute = (method, path, config) => {
172
178
  const apiAny = api;
173
- const routeKey = `${method} ${path}`;
179
+ const normalizedPath = normalizeApiGatewayPath(path);
180
+ const routeKey = `${method} ${normalizedPath}`;
174
181
  const asAny = config.handler;
175
182
  const handlerInput = typeof asAny === "string" ? asAny : asAny && typeof asAny.arn !== "undefined" ? asAny.arn : asAny && typeof asAny.handler === "string" ? asAny.handler : asAny === void 0 ? void 0 : (() => {
176
183
  throw new Error("Unsupported handler type: provide a handler string, FunctionArgs, or a Function ARN/output");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sst-http",
3
- "version": "0.7.0",
3
+ "version": "1.3.0",
4
4
  "description": "Decorator-based routing for SST v3 with a single Lambda and Firebase JWT authorizer.",
5
5
  "license": "MIT",
6
6
  "author": "",