sst-http 0.4.1 → 1.0.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.
- package/dist/index.cjs +31 -10
- package/dist/index.d.cts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +30 -10
- package/package.json +1 -1
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,
|
|
@@ -166,6 +167,7 @@ function normalizeRoutePath(path) {
|
|
|
166
167
|
}
|
|
167
168
|
|
|
168
169
|
// src/runtime.ts
|
|
170
|
+
var HTTP_ERROR_MARKER = Symbol.for("sst-http.HttpError");
|
|
169
171
|
var HttpError = class extends Error {
|
|
170
172
|
statusCode;
|
|
171
173
|
headers;
|
|
@@ -173,6 +175,7 @@ var HttpError = class extends Error {
|
|
|
173
175
|
constructor(statusCode, message, options2) {
|
|
174
176
|
super(message);
|
|
175
177
|
this.name = "HttpError";
|
|
178
|
+
Object.defineProperty(this, HTTP_ERROR_MARKER, { value: true });
|
|
176
179
|
this.statusCode = statusCode;
|
|
177
180
|
this.headers = options2?.headers;
|
|
178
181
|
this.details = options2?.details;
|
|
@@ -299,17 +302,21 @@ function buildHandlerArguments(entry, ctx, getBody) {
|
|
|
299
302
|
break;
|
|
300
303
|
}
|
|
301
304
|
case "query": {
|
|
302
|
-
args[meta.index] = ctx.query;
|
|
305
|
+
args[meta.index] = meta.name ? ctx.query[meta.name] : ctx.query;
|
|
303
306
|
break;
|
|
304
307
|
}
|
|
305
308
|
case "param": {
|
|
306
|
-
args[meta.index] = ctx.params;
|
|
309
|
+
args[meta.index] = meta.name ? ctx.params[meta.name] : ctx.params;
|
|
307
310
|
break;
|
|
308
311
|
}
|
|
309
312
|
case "headers": {
|
|
310
313
|
args[meta.index] = ctx.headers;
|
|
311
314
|
break;
|
|
312
315
|
}
|
|
316
|
+
case "header": {
|
|
317
|
+
args[meta.index] = meta.name ? ctx.headers[meta.name.toLowerCase()] : ctx.headers;
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
313
320
|
case "req": {
|
|
314
321
|
args[meta.index] = ctx.event;
|
|
315
322
|
break;
|
|
@@ -408,8 +415,17 @@ function extractAuthClaims(event, entry) {
|
|
|
408
415
|
const claims = ctxV2?.authorizer?.jwt?.claims || ctxV1?.authorizer?.claims;
|
|
409
416
|
return claims ?? void 0;
|
|
410
417
|
}
|
|
418
|
+
function isHttpError(error) {
|
|
419
|
+
if (!error || typeof error !== "object") {
|
|
420
|
+
return false;
|
|
421
|
+
}
|
|
422
|
+
const marker = error[HTTP_ERROR_MARKER] === true;
|
|
423
|
+
const named = error.name === "HttpError";
|
|
424
|
+
const status = typeof error.statusCode === "number";
|
|
425
|
+
return status && (marker || named);
|
|
426
|
+
}
|
|
411
427
|
function handleError(error, preferV2) {
|
|
412
|
-
if (error
|
|
428
|
+
if (isHttpError(error)) {
|
|
413
429
|
return formatResponse({
|
|
414
430
|
statusCode: error.statusCode,
|
|
415
431
|
headers: {
|
|
@@ -498,13 +514,14 @@ function createRouteDecorator(method) {
|
|
|
498
514
|
registerRoute(handler, method, path);
|
|
499
515
|
};
|
|
500
516
|
}
|
|
501
|
-
function createParameterDecorator(type,
|
|
517
|
+
function createParameterDecorator(type, options2) {
|
|
502
518
|
return (target, propertyKey, parameterIndex) => {
|
|
503
519
|
const handler = resolveHandler(target, propertyKey);
|
|
504
520
|
registerParameter(handler, {
|
|
505
521
|
index: parameterIndex,
|
|
506
522
|
type,
|
|
507
|
-
schema
|
|
523
|
+
schema: options2?.schema,
|
|
524
|
+
name: options2?.name
|
|
508
525
|
});
|
|
509
526
|
};
|
|
510
527
|
}
|
|
@@ -525,17 +542,20 @@ function Auth() {
|
|
|
525
542
|
return createParameterDecorator("auth");
|
|
526
543
|
}
|
|
527
544
|
function Body(schema) {
|
|
528
|
-
return createParameterDecorator("body", schema);
|
|
545
|
+
return createParameterDecorator("body", { schema });
|
|
529
546
|
}
|
|
530
|
-
function Query() {
|
|
531
|
-
return createParameterDecorator("query");
|
|
547
|
+
function Query(name) {
|
|
548
|
+
return createParameterDecorator("query", { name });
|
|
532
549
|
}
|
|
533
|
-
function Param() {
|
|
534
|
-
return createParameterDecorator("param");
|
|
550
|
+
function Param(name) {
|
|
551
|
+
return createParameterDecorator("param", { name });
|
|
535
552
|
}
|
|
536
553
|
function Headers() {
|
|
537
554
|
return createParameterDecorator("headers");
|
|
538
555
|
}
|
|
556
|
+
function Header(name) {
|
|
557
|
+
return createParameterDecorator("header", { name });
|
|
558
|
+
}
|
|
539
559
|
function Req() {
|
|
540
560
|
return createParameterDecorator("req");
|
|
541
561
|
}
|
|
@@ -550,6 +570,7 @@ function Res() {
|
|
|
550
570
|
FirebaseAuth,
|
|
551
571
|
Get,
|
|
552
572
|
Head,
|
|
573
|
+
Header,
|
|
553
574
|
Headers,
|
|
554
575
|
HttpError,
|
|
555
576
|
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
|
@@ -119,6 +119,7 @@ function normalizeRoutePath(path) {
|
|
|
119
119
|
}
|
|
120
120
|
|
|
121
121
|
// src/runtime.ts
|
|
122
|
+
var HTTP_ERROR_MARKER = Symbol.for("sst-http.HttpError");
|
|
122
123
|
var HttpError = class extends Error {
|
|
123
124
|
statusCode;
|
|
124
125
|
headers;
|
|
@@ -126,6 +127,7 @@ var HttpError = class extends Error {
|
|
|
126
127
|
constructor(statusCode, message, options2) {
|
|
127
128
|
super(message);
|
|
128
129
|
this.name = "HttpError";
|
|
130
|
+
Object.defineProperty(this, HTTP_ERROR_MARKER, { value: true });
|
|
129
131
|
this.statusCode = statusCode;
|
|
130
132
|
this.headers = options2?.headers;
|
|
131
133
|
this.details = options2?.details;
|
|
@@ -252,17 +254,21 @@ function buildHandlerArguments(entry, ctx, getBody) {
|
|
|
252
254
|
break;
|
|
253
255
|
}
|
|
254
256
|
case "query": {
|
|
255
|
-
args[meta.index] = ctx.query;
|
|
257
|
+
args[meta.index] = meta.name ? ctx.query[meta.name] : ctx.query;
|
|
256
258
|
break;
|
|
257
259
|
}
|
|
258
260
|
case "param": {
|
|
259
|
-
args[meta.index] = ctx.params;
|
|
261
|
+
args[meta.index] = meta.name ? ctx.params[meta.name] : ctx.params;
|
|
260
262
|
break;
|
|
261
263
|
}
|
|
262
264
|
case "headers": {
|
|
263
265
|
args[meta.index] = ctx.headers;
|
|
264
266
|
break;
|
|
265
267
|
}
|
|
268
|
+
case "header": {
|
|
269
|
+
args[meta.index] = meta.name ? ctx.headers[meta.name.toLowerCase()] : ctx.headers;
|
|
270
|
+
break;
|
|
271
|
+
}
|
|
266
272
|
case "req": {
|
|
267
273
|
args[meta.index] = ctx.event;
|
|
268
274
|
break;
|
|
@@ -361,8 +367,17 @@ function extractAuthClaims(event, entry) {
|
|
|
361
367
|
const claims = ctxV2?.authorizer?.jwt?.claims || ctxV1?.authorizer?.claims;
|
|
362
368
|
return claims ?? void 0;
|
|
363
369
|
}
|
|
370
|
+
function isHttpError(error) {
|
|
371
|
+
if (!error || typeof error !== "object") {
|
|
372
|
+
return false;
|
|
373
|
+
}
|
|
374
|
+
const marker = error[HTTP_ERROR_MARKER] === true;
|
|
375
|
+
const named = error.name === "HttpError";
|
|
376
|
+
const status = typeof error.statusCode === "number";
|
|
377
|
+
return status && (marker || named);
|
|
378
|
+
}
|
|
364
379
|
function handleError(error, preferV2) {
|
|
365
|
-
if (error
|
|
380
|
+
if (isHttpError(error)) {
|
|
366
381
|
return formatResponse({
|
|
367
382
|
statusCode: error.statusCode,
|
|
368
383
|
headers: {
|
|
@@ -451,13 +466,14 @@ function createRouteDecorator(method) {
|
|
|
451
466
|
registerRoute(handler, method, path);
|
|
452
467
|
};
|
|
453
468
|
}
|
|
454
|
-
function createParameterDecorator(type,
|
|
469
|
+
function createParameterDecorator(type, options2) {
|
|
455
470
|
return (target, propertyKey, parameterIndex) => {
|
|
456
471
|
const handler = resolveHandler(target, propertyKey);
|
|
457
472
|
registerParameter(handler, {
|
|
458
473
|
index: parameterIndex,
|
|
459
474
|
type,
|
|
460
|
-
schema
|
|
475
|
+
schema: options2?.schema,
|
|
476
|
+
name: options2?.name
|
|
461
477
|
});
|
|
462
478
|
};
|
|
463
479
|
}
|
|
@@ -478,17 +494,20 @@ function Auth() {
|
|
|
478
494
|
return createParameterDecorator("auth");
|
|
479
495
|
}
|
|
480
496
|
function Body(schema) {
|
|
481
|
-
return createParameterDecorator("body", schema);
|
|
497
|
+
return createParameterDecorator("body", { schema });
|
|
482
498
|
}
|
|
483
|
-
function Query() {
|
|
484
|
-
return createParameterDecorator("query");
|
|
499
|
+
function Query(name) {
|
|
500
|
+
return createParameterDecorator("query", { name });
|
|
485
501
|
}
|
|
486
|
-
function Param() {
|
|
487
|
-
return createParameterDecorator("param");
|
|
502
|
+
function Param(name) {
|
|
503
|
+
return createParameterDecorator("param", { name });
|
|
488
504
|
}
|
|
489
505
|
function Headers() {
|
|
490
506
|
return createParameterDecorator("headers");
|
|
491
507
|
}
|
|
508
|
+
function Header(name) {
|
|
509
|
+
return createParameterDecorator("header", { name });
|
|
510
|
+
}
|
|
492
511
|
function Req() {
|
|
493
512
|
return createParameterDecorator("req");
|
|
494
513
|
}
|
|
@@ -502,6 +521,7 @@ export {
|
|
|
502
521
|
FirebaseAuth,
|
|
503
522
|
Get,
|
|
504
523
|
Head,
|
|
524
|
+
Header,
|
|
505
525
|
Headers,
|
|
506
526
|
HttpError,
|
|
507
527
|
Options,
|