Skip to content

Commit 5fa5685

Browse files
fix: add response modules
1 parent f252beb commit 5fa5685

8 files changed

+165
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { NgModule } from '@angular/core'
2+
import { ResponseService } from './browser.response.service'
3+
4+
// tslint:disable-next-line:no-class
5+
@NgModule({
6+
providers: [ResponseService]
7+
})
8+
export class ResponseBrowserModule {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Injectable } from '@angular/core'
2+
import { IResponseService } from './common'
3+
4+
// tslint:disable:no-this
5+
// tslint:disable:no-object-mutation
6+
// tslint:disable-next-line:no-class
7+
@Injectable()
8+
export class ResponseService implements IResponseService {
9+
set(): void {
10+
// noop on browser
11+
}
12+
13+
ok(): void {
14+
// noop on browser
15+
}
16+
17+
badRequest(): void {
18+
// noop on browser
19+
}
20+
21+
unauthorized(): void {
22+
// noop on browser
23+
}
24+
25+
paymentRequired(): void {
26+
// noop on browser
27+
}
28+
29+
forbidden(): void {
30+
// noop on browser
31+
}
32+
33+
notFound(): void {
34+
// noop on browser
35+
}
36+
37+
error(): void {
38+
// noop on browser
39+
}
40+
41+
notImplemented(): void {
42+
// noop on browser
43+
}
44+
}

src/modules/response/browser.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { ResponseBrowserModule } from './browser.response.module'
2+
export { ResponseService } from './browser.response.service'

src/modules/response/common.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export interface IResponseService {
2+
readonly set: (code: number, message?: string) => void
3+
readonly ok: (message?: string) => void
4+
readonly badRequest: (message?: string) => void
5+
readonly unauthorized: (message?: string) => void
6+
readonly paymentRequired: (message?: string) => void
7+
readonly forbidden: (message?: string) => void
8+
readonly notFound: (message?: string) => void
9+
readonly error: (message?: string) => void
10+
readonly notImplemented: (message?: string) => void
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { NgModule } from '@angular/core'
2+
import { ResponseService } from './browser.response.service'
3+
import { ServerResponseService } from './server.response.service'
4+
5+
// tslint:disable-next-line:no-class
6+
@NgModule({
7+
providers: [{ provide: ResponseService, useClass: ServerResponseService }]
8+
})
9+
export class ResponseServerModule {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { RESPONSE } from '@nguniversal/express-engine/tokens'
2+
import { Inject, Injectable } from '@angular/core'
3+
import { IResponseService } from './common'
4+
import * as express from 'express'
5+
6+
// tslint:disable:no-this
7+
// tslint:disable:no-object-mutation
8+
// tslint:disable-next-line:no-class
9+
@Injectable()
10+
export class ServerResponseService implements IResponseService {
11+
constructor(@Inject(RESPONSE) private response: express.Response) {}
12+
13+
set(code: number, message?: string): void {
14+
this.response.statusCode = code
15+
this.response.statusMessage = message || ''
16+
}
17+
18+
ok(): void {
19+
this.set(200)
20+
}
21+
22+
badRequest(message = 'Bad Request'): void {
23+
this.set(400, message)
24+
}
25+
26+
unauthorized(message = 'Unauthorized'): void {
27+
this.set(401, message)
28+
}
29+
30+
paymentRequired(message = 'Payment Required'): void {
31+
this.set(402, message)
32+
}
33+
34+
forbidden(message = 'Forbidden'): void {
35+
this.set(403, message)
36+
}
37+
38+
notFound(message = 'Not Found'): void {
39+
this.set(404, message)
40+
}
41+
42+
error(message = 'Internal Server Error'): void {
43+
this.set(500, message)
44+
}
45+
46+
notImplemented(message = 'Not Implemented'): void {
47+
this.set(501, message)
48+
}
49+
}

src/modules/response/server.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { ResponseServerModule } from './server.response.module'
2+
export { ServerResponseService } from './server.response.service'

src/modules/util/header.service.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { RESPONSE } from '@nguniversal/express-engine/tokens'
2+
import { Inject, Injectable } from '@angular/core'
3+
import * as express from 'express'
4+
5+
// tslint:disable:no-this
6+
// tslint:disable:no-object-mutation
7+
// tslint:disable-next-line:no-class
8+
@Injectable()
9+
export class HeaderService {
10+
constructor(@Inject(RESPONSE) private response: express.Response) {}
11+
getHeader(key: string): string | undefined {
12+
return this.response.getHeader(key) as string | undefined
13+
}
14+
15+
setHeader(key: string, value: string): void {
16+
this.response.header(key, value)
17+
}
18+
19+
setHeaders(dictionary: { readonly [key: string]: string }): void {
20+
Object.keys(dictionary).forEach(key => this.setHeader(key, dictionary[key]))
21+
}
22+
23+
removeHeader(key: string): void {
24+
this.response.removeHeader(key)
25+
}
26+
27+
appendHeader(key: string, value: string, delimiter = ','): void {
28+
const current = this.getHeader(key)
29+
30+
// tslint:disable-next-line:no-if-statement
31+
if (!current) {
32+
this.setHeader(key, value)
33+
} else {
34+
const newValue = [...current.split(delimiter), value]
35+
.filter((el, i, a) => i === a.indexOf(el))
36+
.join(delimiter)
37+
this.response.header(key, newValue)
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)