koa#DefaultContext TypeScript Examples
The following examples show how to use
koa#DefaultContext.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaseContext.ts From uni-cloud-router with Apache License 2.0 | 6 votes |
export function createContext<StateT = DefaultState, CustomT = DefaultContext>(
config: Record<string, any>,
event: UniCloudEvent,
context: UniCloudContext,
serviceDir: string,
controllerDir: string
) {
const ctx = {
state: {},
event,
context,
} as Context<StateT, CustomT>
// utils
ctx.config = config
ctx.service = noader(serviceDir, ctx)
ctx.controller = noader(controllerDir, ctx)
// request
ctx.query = Object.create(null)
ctx.data = event.data || Object.create(null)
// response
ctx.status = 200
ctx.headers = Object.create(null)
ctx.throw = (code?: string | number, message?: string) => {
if (message) {
throw {
code,
message,
}
}
throw { code: FAILED_CODE, message: code }
}
// uniCloud
if (runInUniCloud) {
ctx.db = uniCloud.database()
ctx.curl = uniCloud.httpclient.request.bind(uniCloud.httpclient)
ctx.httpclient = uniCloud.httpclient
}
return ctx
}