Wrappers and cookies
Wrappers#
A wrapper is shared processing that wraps the request and the response. Declare it in init.comat (init / defaults / wrappers), write the implementation in each language's code, and pass it to init.
Use the same name as wrapper name in init.comat. The generated Wrappers type tells you about a missing wrapper or a different type (except in Go).
TypeScript#
// Implement a wrapper as a value of type ComatypeWrap and export it (corresponds to the `wrapper auth` of init.comat)
import type { ComatypeWrap } from '@comatype/typescript'
// Generated from init.comat: the ctx type (AuthCtx) and the statuses a wrapper can return (s401)
import { s401, type AuthCtx } from '@/comatype/init'
import { db } from '../db'
// ComatypeWrap<AuthCtx>: the type of values that can be added to ctx. On endpoints with `wrap auth` on, a handler can read req.ctx.user, typed
export const auth: ComatypeWrap<AuthCtx> = async (req, next) => {
// A session cookie (browsers) or Authorization: Bearer (other clients)
const bearer = req.headers.authorization?.replace(/^Bearer /i, '')
const user = db.bySession(req.cookies.session ?? bearer)
if (!user) {
return s401({ error: { code: 'unauthorized', message: 'login required' } })
}
return next({ ...req, ctx: { ...req.ctx, user } })
}- On the server, add to ctx with
next({ ...req, ctx: { ...req.ctx, user } })and hand it on to what follows and to the handler. - You can return a status the wrapper declared, such as
s401(...), and stop right there. - On the client, rewrite the request headers and call
next(req).
// Implement a wrapper as a value of type ComatypeWrap and export it (corresponds to the `wrapper auth` of init.comat)
import type { ComatypeWrap } from '@comatype/typescript'
export const CLIENT_VERSION = '1.0.0'
// A wrapper receives the request and `next` and returns a response (both can be rewritten)
// The session travels in a cookie (kept and sent by the cookie.jar of init), so only headers common to every request are added here
export const auth: ComatypeWrap = async (req, next) => {
// headerReq / header (X-Client-Version / X-Request-Id) of defaults.comat
req.headers['x-client-version'] = CLIENT_VERSION
req.headers['x-request-id'] ??= crypto.randomUUID()
return next(req)
}Go#
var Auth s.CoatWrap[comatype_init.AuthCtx] = func(req *s.RawRequest[comatype_init.AuthCtx], next s.Next[comatype_init.AuthCtx]) s.RawResponse {
user, ok := verifySession(req.Cookie("session"))
if !ok {
return comatype_init.S401(&comatype_init.S401Res{Code: "unauthorized", Message: "login required"})
}
req.Ctx = comatype_init.AuthCtx{User: user}
return next(req)
}On the client it is c.ComatypeWrap (func(req *c.RawRequest, next c.Next) (*c.RawResponse, error)).
Python#
async def _auth(req, next):
user = await verify_session(req.cookies.get("session"))
if user is None:
return S401(error=S401.Res(code="unauthorized", message="login required"))
req.ctx = AuthCtx(user=user)
return await next(req)
auth = ComatypeWrap[AuthCtx](_auth)How they work#
- Wrappers nest from outside to inside. The outer one runs first, and the response goes back from inside to outside.
- What a wrapper sees is the raw request and response that have no per-endpoint types (the wire form).
- A response a wrapper makes with
s401()is validated and converted on the spot, and looks the same to the wrappers outside it. - If a wrapper is
wrapped but not passed toinit, the client gives an error before any network call, and the server gives500.
Cookies#
Issuing (server)#
In an endpoint with cookie on, pass it when you build the response. The attributes (httpOnly secure sameSite) come from init.comat. Headers and cookies shows how to write it in three languages.
- A string →
Set-Cookiewith that value maxAgeexpirespathdomain→ change only for that issue- Delete →
Max-Age=0and a pastExpires - A cookie that is not declared, or passing one in a
cookie offendpoint, gives500
Receiving (server)#
Read it from req.cookies in a handler or in a wrapper.
Keeping (client)#
A browser uses credentials: 'include' and leaves it to the browser. Environments that do not keep cookies, such as Node, take a cookie jar.
| Language | How to pass it |
|---|---|
| TypeScript | c.init({ cookie: { jar: createCookieJar() } }) |
| Go | c.Init(c.Config{CookieJar: c.NewCookieJar()}) |
| Python | c.init(cookies=True) (or CookieJar()) |
The jar keeps the Set-Cookie of responses from cookie on endpoints and sends it back in the Cookie header of the next request. It assumes the API is a single origin, ignores Domain, and looks only at Path and expiry. It keeps HttpOnly cookies too.