@extk/expressive 0.5.3 → 0.5.5
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/README.md +12 -4
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +24 -3
- package/dist/index.mjs +24 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -60,9 +60,13 @@ const swaggerDoc = swaggerBuilder()
|
|
|
60
60
|
.withServers([{ url: 'http://localhost:3000' }])
|
|
61
61
|
.get();
|
|
62
62
|
|
|
63
|
-
// 3. Build the Express app
|
|
63
|
+
// 3. Build the Express app
|
|
64
64
|
const app = expressiveServer()
|
|
65
|
-
.
|
|
65
|
+
.withHelmet()
|
|
66
|
+
.withQs()
|
|
67
|
+
.withMorgan()
|
|
68
|
+
.withSwagger({ path: '/api-docs', doc: swaggerDoc })
|
|
69
|
+
.get();
|
|
66
70
|
|
|
67
71
|
// 4. Define routes — they auto-register in the OpenAPI spec
|
|
68
72
|
const { router, addRoute } = expressiveRouter({
|
|
@@ -203,7 +207,10 @@ export const loginSchema = z.object({
|
|
|
203
207
|
import z from 'zod';
|
|
204
208
|
|
|
205
209
|
const app = expressiveServer()
|
|
206
|
-
.
|
|
210
|
+
.withHelmet()
|
|
211
|
+
.withQs()
|
|
212
|
+
.withMorgan()
|
|
213
|
+
.withSwagger({
|
|
207
214
|
doc: swaggerBuilder()
|
|
208
215
|
.withInfo({ title: 'My API' })
|
|
209
216
|
.withServers([{ url: 'http://localhost:3000/api' }])
|
|
@@ -211,7 +218,8 @@ const app = expressiveServer()
|
|
|
211
218
|
.withSecuritySchemes({ auth: SWG.securitySchemes.BearerAuth() })
|
|
212
219
|
.withDefaultSecurity([SWG.security('auth')])
|
|
213
220
|
.get(),
|
|
214
|
-
})
|
|
221
|
+
})
|
|
222
|
+
.get();
|
|
215
223
|
```
|
|
216
224
|
|
|
217
225
|
**3. Reference them in routes with `SWG.jsonSchemaRef`:**
|
package/dist/index.d.mts
CHANGED
|
@@ -175,7 +175,6 @@ declare class ServerBuilder {
|
|
|
175
175
|
withMorgan(format?: string, // TODO: FormatFn
|
|
176
176
|
options?: Parameters<typeof morgan>[1]): this;
|
|
177
177
|
withSwagger(swagger: SwaggerOptions, ...handlers: ExpressHandler[]): this;
|
|
178
|
-
withDefaults(swagger: SwaggerOptions): express__default.Express;
|
|
179
178
|
}
|
|
180
179
|
|
|
181
180
|
declare class ApiError extends Error {
|
|
@@ -288,6 +287,7 @@ declare function bootstrap(container: Container): {
|
|
|
288
287
|
silently: (fn: () => Promise<void> | void) => Promise<void>;
|
|
289
288
|
notFoundMiddleware: (_req: express.Request, res: express.Response, _next: express.NextFunction) => void;
|
|
290
289
|
getErrorHandlerMiddleware: (errorMapper?: (err: Error & Record<string, unknown>) => ApiError | null | undefined) => (err: Error & Record<string, unknown>, req: express.Request, res: express.Response, _next: express.NextFunction) => Promise<void>;
|
|
290
|
+
getBasicAuthMiddleware: (basicAuthBase64: string, basicRealm?: string) => (req: express.Request, res: express.Response, next: express.NextFunction) => void;
|
|
291
291
|
expressiveServer: (configs?: {
|
|
292
292
|
app?: express.Express;
|
|
293
293
|
}) => ServerBuilder;
|
package/dist/index.d.ts
CHANGED
|
@@ -175,7 +175,6 @@ declare class ServerBuilder {
|
|
|
175
175
|
withMorgan(format?: string, // TODO: FormatFn
|
|
176
176
|
options?: Parameters<typeof morgan>[1]): this;
|
|
177
177
|
withSwagger(swagger: SwaggerOptions, ...handlers: ExpressHandler[]): this;
|
|
178
|
-
withDefaults(swagger: SwaggerOptions): express__default.Express;
|
|
179
178
|
}
|
|
180
179
|
|
|
181
180
|
declare class ApiError extends Error {
|
|
@@ -288,6 +287,7 @@ declare function bootstrap(container: Container): {
|
|
|
288
287
|
silently: (fn: () => Promise<void> | void) => Promise<void>;
|
|
289
288
|
notFoundMiddleware: (_req: express.Request, res: express.Response, _next: express.NextFunction) => void;
|
|
290
289
|
getErrorHandlerMiddleware: (errorMapper?: (err: Error & Record<string, unknown>) => ApiError | null | undefined) => (err: Error & Record<string, unknown>, req: express.Request, res: express.Response, _next: express.NextFunction) => Promise<void>;
|
|
290
|
+
getBasicAuthMiddleware: (basicAuthBase64: string, basicRealm?: string) => (req: express.Request, res: express.Response, next: express.NextFunction) => void;
|
|
291
291
|
expressiveServer: (configs?: {
|
|
292
292
|
app?: express.Express;
|
|
293
293
|
}) => ServerBuilder;
|
package/dist/index.js
CHANGED
|
@@ -228,9 +228,6 @@ var ServerBuilder = class {
|
|
|
228
228
|
}));
|
|
229
229
|
return this;
|
|
230
230
|
}
|
|
231
|
-
withDefaults(swagger) {
|
|
232
|
-
return this.withHelmet().withQs().withMorgan().withSwagger(swagger).get();
|
|
233
|
-
}
|
|
234
231
|
};
|
|
235
232
|
function buildExpressive(container, swaggerDoc) {
|
|
236
233
|
return {
|
|
@@ -432,6 +429,30 @@ var buildMiddleware = (container) => {
|
|
|
432
429
|
}
|
|
433
430
|
res.status(finalError.httpStatusCode).json(new ApiErrorResponse(finalError.message, finalError.code, finalError.data));
|
|
434
431
|
};
|
|
432
|
+
},
|
|
433
|
+
getBasicAuthMiddleware: (basicAuthBase64, basicRealm) => {
|
|
434
|
+
return (req, res, next) => {
|
|
435
|
+
try {
|
|
436
|
+
const token = req.header("authorization");
|
|
437
|
+
if (!token || token.indexOf("Basic ") === -1) {
|
|
438
|
+
throw new UserUnauthorizedError("Missing Authorization Header");
|
|
439
|
+
}
|
|
440
|
+
const credentials = token.split(" ")[1];
|
|
441
|
+
if (basicAuthBase64 !== credentials) {
|
|
442
|
+
throw new UserUnauthorizedError("Invalid Authentication Credentials");
|
|
443
|
+
}
|
|
444
|
+
next();
|
|
445
|
+
} catch (error) {
|
|
446
|
+
if (basicRealm) {
|
|
447
|
+
res.set("WWW-Authenticate", `Basic realm="${basicRealm}"`);
|
|
448
|
+
}
|
|
449
|
+
if (error instanceof ApiError) {
|
|
450
|
+
return next(error);
|
|
451
|
+
}
|
|
452
|
+
logger.error(error);
|
|
453
|
+
return next(new UserUnauthorizedError());
|
|
454
|
+
}
|
|
455
|
+
};
|
|
435
456
|
}
|
|
436
457
|
};
|
|
437
458
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -167,9 +167,6 @@ var ServerBuilder = class {
|
|
|
167
167
|
}));
|
|
168
168
|
return this;
|
|
169
169
|
}
|
|
170
|
-
withDefaults(swagger) {
|
|
171
|
-
return this.withHelmet().withQs().withMorgan().withSwagger(swagger).get();
|
|
172
|
-
}
|
|
173
170
|
};
|
|
174
171
|
function buildExpressive(container, swaggerDoc) {
|
|
175
172
|
return {
|
|
@@ -371,6 +368,30 @@ var buildMiddleware = (container) => {
|
|
|
371
368
|
}
|
|
372
369
|
res.status(finalError.httpStatusCode).json(new ApiErrorResponse(finalError.message, finalError.code, finalError.data));
|
|
373
370
|
};
|
|
371
|
+
},
|
|
372
|
+
getBasicAuthMiddleware: (basicAuthBase64, basicRealm) => {
|
|
373
|
+
return (req, res, next) => {
|
|
374
|
+
try {
|
|
375
|
+
const token = req.header("authorization");
|
|
376
|
+
if (!token || token.indexOf("Basic ") === -1) {
|
|
377
|
+
throw new UserUnauthorizedError("Missing Authorization Header");
|
|
378
|
+
}
|
|
379
|
+
const credentials = token.split(" ")[1];
|
|
380
|
+
if (basicAuthBase64 !== credentials) {
|
|
381
|
+
throw new UserUnauthorizedError("Invalid Authentication Credentials");
|
|
382
|
+
}
|
|
383
|
+
next();
|
|
384
|
+
} catch (error) {
|
|
385
|
+
if (basicRealm) {
|
|
386
|
+
res.set("WWW-Authenticate", `Basic realm="${basicRealm}"`);
|
|
387
|
+
}
|
|
388
|
+
if (error instanceof ApiError) {
|
|
389
|
+
return next(error);
|
|
390
|
+
}
|
|
391
|
+
logger.error(error);
|
|
392
|
+
return next(new UserUnauthorizedError());
|
|
393
|
+
}
|
|
394
|
+
};
|
|
374
395
|
}
|
|
375
396
|
};
|
|
376
397
|
};
|