Skip to content

Commit 1809a50

Browse files
author
Bruno Castro
committed
Make cors fully configurable and fix error properties naming
1 parent 3356f73 commit 1809a50

File tree

11 files changed

+25
-32
lines changed

11 files changed

+25
-32
lines changed

docker-compose.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: '3.9'
22

33
services:
44
mongodb:
5-
container_name: nab-mongodb
5+
container_name: blog-mongodb
66
image: mongo:4.2
77
ports:
88
- 27017:27017
@@ -13,17 +13,17 @@ services:
1313
MONGO_INITDB_ROOT_PASSWORD: blog
1414

1515
mongo-express:
16-
container_name: nab-mongo-express
16+
container_name: blog-mongo-express
1717
image: mongo-express
1818
depends_on:
1919
- mongodb
2020
ports:
2121
- 8081:8081
2222
environment:
23-
ME_CONFIG_MONGODB_URL: mongodb://blog:blog@nab-mongodb:27017
23+
ME_CONFIG_MONGODB_URL: mongodb://blog:blog@blog-mongodb:27017
2424

2525
dev:
26-
container_name: nab-dev
26+
container_name: blog-dev
2727
build:
2828
context: .
2929
dockerfile: ./docker/Dockerfile.dev

src/_boot/server.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { requestContainer } from '@/_lib/http/middlewares/requestContainer';
66
import { statusHandler } from '@/_lib/http/middlewares/statusHandler';
77
import { errorConverters } from '@/_sharedKernel/interface/http/ErrorConverters';
88
import { asValue } from 'awilix';
9-
import cors from 'cors';
9+
import cors, { CorsOptions } from 'cors';
1010
import express, { Application, json, Router, urlencoded } from 'express';
1111
import helmet from 'helmet';
1212
import { createServer, Server } from 'http';
@@ -15,11 +15,7 @@ type ServerConfig = {
1515
http: {
1616
host: string;
1717
port: number;
18-
cors?:
19-
| boolean
20-
| {
21-
allowedOrigins: string | string[];
22-
};
18+
cors?: boolean | CorsOptions;
2319
};
2420
};
2521

@@ -42,15 +38,7 @@ const server = makeModule(
4238
server.use(shutdownHandler());
4339

4440
if (http.cors) {
45-
server.use((req, res, next) => {
46-
return cors({
47-
allowedHeaders:
48-
'accept, accept-encoding, origin, referer, sec-fetch-*, user-agent, content-type, authorization',
49-
credentials: true,
50-
origin: typeof http.cors === 'boolean' ? req.get('origin') : http.cors?.allowedOrigins,
51-
methods: '*',
52-
})(req, res, next);
53-
});
41+
server.use(cors(typeof http.cors !== 'boolean' ? http.cors : {}));
5442
}
5543

5644
server.use(httpLogger());

src/_lib/PartializeProperties.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type PartializeProperties<Type, Properties extends keyof Type> = Omit<Type, Properties> &
2+
Partial<Pick<Type, Properties>>;
3+
4+
export { PartializeProperties };

src/_lib/errors/BadRequestError.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { makePredicate } from '@/_lib/Predicate';
44
namespace BadRequestError {
55
const type = Symbol();
66
const name = 'BadRequestError';
7-
const _message = 'Bad Request';
7+
const defaultMessage = 'Bad Request';
88

9-
export const create = (message: string = _message, code: string = name): Exception =>
9+
export const create = (message: string = defaultMessage, code: string = name): Exception =>
1010
new BaseError({ type, name, code, message });
1111

1212
export const is = makePredicate<Exception>(type);

src/_lib/errors/BaseError.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { PartializeProperties } from '@/_lib/PartializeProperties';
2+
13
type Exception<M = any> = Readonly<{
24
name: string;
35
type: symbol;
@@ -6,7 +8,7 @@ type Exception<M = any> = Readonly<{
68
meta?: M;
79
}>;
810

9-
type Props<M = any> = Omit<Exception<M>, 'name'> & { name?: string };
11+
type Props<M = any> = PartializeProperties<Exception<M>, 'name'>;
1012

1113
class BaseError<M = any> extends Error implements Exception<M> {
1214
public readonly type: symbol;

src/_lib/errors/ForbiddenError.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { makePredicate } from '@/_lib/Predicate';
44
namespace ForbiddenError {
55
const type = Symbol();
66
const name = 'ForbiddenError';
7-
const _message = 'Forbidden';
7+
const defaultMessage = 'Forbidden';
88

9-
export const create = (message: string = _message, code: string = name): Exception =>
9+
export const create = (message: string = defaultMessage, code: string = name): Exception =>
1010
new BaseError({ type, name, code, message });
1111

1212
export const is = makePredicate<Exception>(type);

src/_lib/errors/NotFoundError.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { makePredicate } from '@/_lib/Predicate';
44
namespace NotFoundError {
55
const type = Symbol();
66
const name = 'NotFoundError';
7-
const _message = 'Not Found';
7+
const defaultMessage = 'Not Found';
88

9-
export const create = (message: string = _message, code: string = name): Exception =>
9+
export const create = (message: string = defaultMessage, code: string = name): Exception =>
1010
new BaseError({ type, name, code, message });
1111

1212
export const is = makePredicate<Exception>(type);

src/_lib/errors/UnauthorizedError.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { makePredicate } from '@/_lib/Predicate';
44
namespace UnauthorizedError {
55
const type = Symbol();
66
const name = 'UnauthorizedError';
7-
const _message = 'Unauthorized';
7+
const defaultMessage = 'Unauthorized';
88

9-
export const create = (message: string = _message, code: string = name): Exception =>
9+
export const create = (message: string = defaultMessage, code: string = name): Exception =>
1010
new BaseError({ type, name, code, message });
1111

1212
export const is = makePredicate<Exception>(type);

src/_lib/repl/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import REPL, { REPLEval, ReplOptions, REPLServer } from 'repl';
22
import vm from 'vm';
3-
import { createServer, Server } from 'net';
3+
import { createServer, Server, Socket } from 'net';
44

55
type REPLProps = {
66
context: Record<string, any>;
@@ -44,7 +44,7 @@ const makeREPL = ({ context, prompt, cli, remote, logger }: REPLProps): REPLInst
4444
return repl;
4545
};
4646

47-
let destroySocket: (...args: any) => void = () => null;
47+
let destroySocket: Socket['destroy'] = () => null;
4848

4949
return {
5050
create,

src/article/application/useCases/PublishArticle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const makePublishArticle =
1919
if (Article.isPublished(article)) {
2020
throw BusinessError.create(
2121
// eslint-disable-next-line max-len
22-
`Can't republish the Article(id=${payload}) because it was already published on ${article.publishedAt.toISOString()}`
22+
`Can't republish the Article(id=${payload}) because it was already published at ${article.publishedAt.toISOString()}`
2323
);
2424
}
2525

0 commit comments

Comments
 (0)