@emmert/context
v0.22.0
Published
Tools for composing context
Downloads
5
Readme
Create Context
Tools for composing application context with middleware.
Architecture
Create apps by concatinating middleware.
Installation
NPMnpm install @emmert/context
Yarnyarn add @emmert/context
Configuration & Setup
Create an express application
export async function createAppContext() {
const context = new AppContext()
const app = express()
const dbConfig = await getDbConfig()
await context.use(
expressMiddleware(app),
resourcesMiddleware({
defaultLanguage: 'en',
resources: async () => {
const resourcesDir = path.join('src', 'resources')
const resources = await loadResourcesFromDirectory(resourcesDir)
return resources
},
}),
typeOrmMiddleware(dbConfig.default),
graphQLMiddleware((appContext: AppContext) => ({
schema: buildFederatedSchema([
{
typeDefs,
resolvers: resolvers(appContext),
},
]),
context: async (express): Promise<RequestContext> => {
const context = new RequestContext()
await context.use(
appContext.asMiddleware,
expressContextMiddleware(express.req),
resourcesMiddlewareForLocale(
express.req.headers['language'] as string,
),
authClient.middleware,
)
return context
},
})),
)