You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to use the verify method on the key stack. I have assigned keys to they app so I know the key stack exists; however, in my middleware the compiler complains when I try to access the verify method.
i.e. ctx.app.keys.verify(foo)
I think it is because the compiler thinks that keys is of type: keyStack | keys[] | undefined.
const claims = await ctx.app.keys.verify(token); //<-- current problem
const queryString = queryStrings[ctx.params.resource][ctx.request.method] //<-- next problem
ctx.assert(queryString, 405, method not allowed for ${ctx.params.resource});
/* finally do the query and return the result */
...
const router = new Router()
router.all('/:resource/:id(\d*)', restAPI)
const app = new Application();
app.keys = ['secret1','secret 2'] // <- should trigger the creation of the KeyStack
I am pretty sure I need to extend the RouterContex to indicate that keys will always be a KeyStack but I don't know how. (Just started using typescript.)
The next problem I think I will have is ctx.params.resource will have a string - but I really want it to be a Resource
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I would like to use the verify method on the key stack. I have assigned keys to they app so I know the key stack exists; however, in my middleware the compiler complains when I try to access the verify method.
i.e. ctx.app.keys.verify(foo)
I think it is because the compiler thinks that keys is of type: keyStack | keys[] | undefined.
Here is the code I am using:
type Resource = 'job' | 'user'
type qs = Record<Resource, {
'GET'?: string,
'PUT'?: string,
'POST'?: string,
'DELETE'?: string
}>
const queryStrings:qs = {
'job':{ 'GET': 'query 1...' },
'user': { 'GET': 'query 2...'
}
}
async function restAPI(ctx: RouterContext<'/:resource(job|user)/:id(\d*)'>) {
const token = ctx.request.headers.get("Authorize")?.replace(/bearer\s*/, '');
ctx.assert(token, 400, 'No bearer token');
const claims = await ctx.app.keys.verify(token); //<-- current problem
const queryString = queryStrings[ctx.params.resource][ctx.request.method] //<-- next problem
ctx.assert(queryString, 405,
method not allowed for ${ctx.params.resource}
);/* finally do the query and return the result */
...
const router = new Router()
router.all('/:resource/:id(\d*)', restAPI)
const app = new Application();
app.keys = ['secret1','secret 2'] // <- should trigger the creation of the KeyStack
app.use(router.routes(), router.allowedMethods());
await app.listen({ port:8080 });
I am pretty sure I need to extend the RouterContex to indicate that keys will always be a KeyStack but I don't know how. (Just started using typescript.)
The next problem I think I will have is ctx.params.resource will have a string - but I really want it to be a Resource
Beta Was this translation helpful? Give feedback.
All reactions