ilokesto

Runtime context

Fetcher injects OpenAPI metadata into ky context for string URL requests.

const api = createFetcher<paths>({
  hooks: {
    beforeRequest: [(_request, options) => {
      const pathTemplate = options.context.openapi?.pathTemplate;
      const method = options.context.openapi?.method;
    }],
  },
});

Shape

type OpenApiContext<Path extends string, Method extends string> = {
  openapi?: {
    pathTemplate?: Path;
    method?: Method;
  };
};

pathTemplate is the original template passed to the client, not the interpolated URL. method is normalized to lowercase at runtime.

Existing context is preserved

If you pass your own ky context, fetcher merges openapi into it instead of replacing the whole object.

api.get('/users/{id}', {
  params: { path: { id: '42' } },
}, {
  context: {
    requestId: crypto.randomUUID(),
  },
});

Use this to combine application tracing fields with fetcher-provided OpenAPI metadata.

On this page