-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3298 from SeedCompany/http-adapter
More HttpAdapter usage
- Loading branch information
Showing
9 changed files
with
92 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,36 @@ | ||
// eslint-disable-next-line @seedcompany/no-restricted-imports | ||
import { HttpAdapterHost as HttpAdapterHostImpl } from '@nestjs/core'; | ||
import { | ||
ExpressAdapter as HttpAdapter, | ||
NestExpressApplication as NestHttpApplication, | ||
NestExpressApplication as BaseApplication, | ||
ExpressAdapter, | ||
} from '@nestjs/platform-express'; | ||
import cookieParser from 'cookie-parser'; | ||
import { ConfigService } from '../config/config.service'; | ||
import type { CorsOptions } from './index'; | ||
import { CookieOptions, IResponse } from './types'; | ||
|
||
export { HttpAdapter, type NestHttpApplication }; | ||
export type NestHttpApplication = BaseApplication & { | ||
configure: (app: BaseApplication, config: ConfigService) => Promise<void>; | ||
}; | ||
|
||
export class HttpAdapterHost extends HttpAdapterHostImpl<HttpAdapter> {} | ||
|
||
export class HttpAdapter extends ExpressAdapter { | ||
async configure(app: BaseApplication, config: ConfigService) { | ||
app.enableCors(config.cors as CorsOptions); // typecast to undo deep readonly | ||
app.use(cookieParser()); | ||
|
||
app.setGlobalPrefix(config.hostUrl$.value.pathname.slice(1)); | ||
|
||
config.applyTimeouts(app.getHttpServer(), config.httpTimeouts); | ||
} | ||
|
||
setCookie( | ||
response: IResponse, | ||
name: string, | ||
value: string, | ||
options: CookieOptions, | ||
) { | ||
response.cookie(name, value, options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,47 @@ | ||
import { Module } from '@nestjs/common'; | ||
// eslint-disable-next-line @seedcompany/no-restricted-imports | ||
import { HttpAdapterHost as HttpAdapterHostImpl } from '@nestjs/core'; | ||
import { HttpAdapterHost } from './http.adapter'; | ||
import { setOf } from '@seedcompany/common'; | ||
import { getParentTypes } from '~/common'; | ||
import { HttpAdapter, HttpAdapterHost } from './http.adapter'; | ||
|
||
@Module({ | ||
providers: [ | ||
{ | ||
provide: HttpAdapterHost, | ||
useExisting: HttpAdapterHostImpl, | ||
}, | ||
{ | ||
provide: HttpAdapter, | ||
inject: [HttpAdapterHost], | ||
useFactory: async (host: HttpAdapterHost) => { | ||
const availableKeys = setOf( | ||
getParentTypes(HttpAdapter).flatMap((cls) => [ | ||
...Object.getOwnPropertyNames(cls.prototype), | ||
...Object.getOwnPropertySymbols(cls.prototype), | ||
]), | ||
); | ||
return new Proxy(host, { | ||
get(_, key, receiver) { | ||
const { httpAdapter } = host; | ||
if (key === 'httpAdapter') { | ||
return httpAdapter; | ||
} | ||
if (key === 'constructor') { | ||
return HttpAdapter.constructor; | ||
} | ||
if (!availableKeys.has(key)) { | ||
return undefined; | ||
} | ||
if (!httpAdapter) { | ||
throw new Error('HttpAdapter is not yet available'); | ||
} | ||
return Reflect.get(httpAdapter, key, receiver); | ||
}, | ||
}); | ||
}, | ||
}, | ||
], | ||
exports: [HttpAdapterHost], | ||
exports: [HttpAdapter, HttpAdapterHost], | ||
}) | ||
export class HttpModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters