Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: default add session & bodyparser support for koa/express/faas (#…
- Loading branch information
1 parent
d23e4a4
commit cdaff31
Showing
75 changed files
with
1,648 additions
and
157 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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { IMidwayApplication, IMidwayFramework } from '../interface'; | ||
import { FrameworkType, Provide, Scope, ScopeEnum } from '@midwayjs/decorator'; | ||
|
||
@Provide() | ||
@Scope(ScopeEnum.Singleton) | ||
export class MidwayApplicationManager { | ||
private globalFrameworkMap = new Map< | ||
string, | ||
IMidwayFramework<any, any, any> | ||
>(); | ||
|
||
private globalFrameworkTypeMap = new WeakMap< | ||
FrameworkType, | ||
IMidwayFramework<any, any, any> | ||
>(); | ||
|
||
public addFramework(namespace, framework: IMidwayFramework<any, any, any>) { | ||
this.globalFrameworkMap.set(namespace, framework); | ||
this.globalFrameworkTypeMap.set(framework.getFrameworkType(), framework); | ||
} | ||
|
||
public getFramework(namespaceOrFrameworkType: string | FrameworkType) { | ||
if (typeof namespaceOrFrameworkType === 'string') { | ||
if (this.globalFrameworkMap.has(namespaceOrFrameworkType)) { | ||
return this.globalFrameworkMap.get(namespaceOrFrameworkType); | ||
} | ||
} else { | ||
if (this.globalFrameworkTypeMap.has(namespaceOrFrameworkType)) { | ||
return this.globalFrameworkTypeMap.get(namespaceOrFrameworkType); | ||
} | ||
} | ||
} | ||
|
||
public getApplication( | ||
namespaceOrFrameworkType: string | FrameworkType | ||
): IMidwayApplication { | ||
if (typeof namespaceOrFrameworkType === 'string') { | ||
if (this.globalFrameworkMap.has(namespaceOrFrameworkType)) { | ||
return this.globalFrameworkMap | ||
.get(namespaceOrFrameworkType) | ||
.getApplication(); | ||
} | ||
} else { | ||
if (this.globalFrameworkTypeMap.has(namespaceOrFrameworkType)) { | ||
return this.globalFrameworkTypeMap | ||
.get(namespaceOrFrameworkType) | ||
.getApplication(); | ||
} | ||
} | ||
} | ||
|
||
public getApplications( | ||
namespaces: Array<string | FrameworkType> | ||
): IMidwayApplication[] { | ||
return namespaces | ||
.map(namespace => { | ||
return this.getApplication(namespace); | ||
}) | ||
.filter(app => { | ||
return !!app; | ||
}); | ||
} | ||
|
||
public getWebLikeApplication(): IMidwayApplication[] { | ||
return this.getApplications(['express', 'koa', 'egg', 'faas']); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { MidwayApplicationManager, BaseFramework } from '../../src'; | ||
import { MidwayFrameworkType } from '@midwayjs/decorator'; | ||
|
||
describe('test/common/applicationManager.test.ts', () => { | ||
it('should test application manager', async () => { | ||
const manager = new MidwayApplicationManager(); | ||
|
||
class CustomFramework1 extends BaseFramework<any, any, any> { | ||
applicationInitialize(options): any { | ||
this.app = {}; | ||
} | ||
|
||
configure(options: any): any { | ||
return {}; | ||
} | ||
|
||
getFrameworkType() { | ||
return MidwayFrameworkType.EMPTY; | ||
} | ||
|
||
run(): Promise<void> { | ||
return Promise.resolve(undefined); | ||
} | ||
} | ||
|
||
const framework = new CustomFramework1({} as any); | ||
await framework.initialize(); | ||
|
||
manager.addFramework('test', framework); | ||
|
||
expect(manager.getApplication('test')).toBeDefined(); | ||
expect(manager.getApplication(MidwayFrameworkType.EMPTY)).toBeDefined(); | ||
expect(manager.getApplication('xxxx')).toBeUndefined(); | ||
expect(manager.getApplications([MidwayFrameworkType.EMPTY, 'test', 'xxx']).length).toEqual(2); | ||
}); | ||
}); |
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
Oops, something went wrong.