Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e64ff6f
commit 650ab4f
Showing
4 changed files
with
53 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Inject, Injectable } from '@dandi/core' | ||
import { HttpMethod, MvcResponse, Route } from '@dandi/mvc' | ||
|
||
import { OptionsHandler } from './options-handler' | ||
|
||
const ALLOW_HEADER = 'Allow' | ||
|
||
@Injectable(OptionsHandler) | ||
export class BasicOptionsHandler implements OptionsHandler { | ||
|
||
public handleRequest(@Inject(Route) route: Route, @Inject(MvcResponse) res: MvcResponse): void { | ||
this.setAllowHeader(ALLOW_HEADER, route, res) | ||
} | ||
|
||
protected setAllowHeader(header: string, route: Route, res: MvcResponse): void { | ||
const methods = new Set(route.siblingMethods) | ||
methods.add(HttpMethod.options) | ||
res.setHeader(ALLOW_HEADER, [...methods.values()].join(',')) | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Inject, Injectable } from '@dandi/core' | ||
import { MvcResponse, Route } from '@dandi/mvc' | ||
|
||
import { BasicOptionsHandler } from './basic.options-handler' | ||
import { OptionsHandler } from './options-handler' | ||
|
||
export enum CorsHeader { | ||
allowOrigin = 'Access-Control-Allow-Origin', | ||
allowMethods = 'Access-Control-Allow-Methods', | ||
allowHeaders = 'Access-Control-Allow-Headers', | ||
maxAge = 'Access-Control-Max-Age', | ||
} | ||
|
||
@Injectable(OptionsHandler) | ||
export class CorsHandler extends BasicOptionsHandler { | ||
|
||
public async handleRequest(@Inject(Route) route: Route, @Inject(MvcResponse) res: MvcResponse): Promise<void> { | ||
this.setAllowHeader(CorsHeader.allowMethods, route, res) | ||
} | ||
|
||
} |
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { InjectionToken } from '@dandi/core' | ||
|
||
import { localOpinionatedToken } from './local-token' | ||
|
||
export interface OptionsHandler { | ||
handleRequest(...args: any[]): void | Promise<void> | ||
} | ||
|
||
export const OptionsHandler: InjectionToken<OptionsHandler> = localOpinionatedToken('OptionsHandler', { | ||
multi: false, | ||
}) |