Predefined operation parameters
The file create-predefined-parameters-request-fn.ts
will be generated by the @openapi-qraft/cli
when the --operation-predefined-parameters
option is used.
In this file, the createPredefinedParametersRequestFn(...)
function is exported, which is used to create a custom requestFn
function that predefines the parameters for the specified operations.
import { requestFn as baseRequestFn } from '@openapi-qraft/react/dist/types';
import { createPredefinedParametersRequestFn } from './api/create-predefined-parameters-request-fn.ts'
const requestFn = createPredefinedParametersRequestFn(
inputPredefinedParameters,
baseRequestFn
);
Argumentsโ
inputPredefinedParameters: InputPredefinedParametersItem[]
- Required predefined parameters for the operationsinputPredefinedParameters.requestPattern
- Required request pattern to match operation methods and paths provided by--operation-predefined-parameters <...patterns>
option, e.g.get,post /approval_policies/{approval_policy_id}/**
inputPredefinedParameters.parameters
- Required predefined parameters for the operationinputPredefinedParameters.parameters.in
- Required parameter locationinputPredefinedParameters.parameters.name
- Required parameter nameinputPredefinedParameters.parameters.value: T | (() => Promise<T>)
- Required parameter value
baseRequestFn
- RequiredrequestFn
function to be wrapped with predefined parameters
Returnsโ
requestFn
function with predefined parameters
Exampleโ
Let's say we generate the API client with the following command:
- npm
- yarn
- pnpm
npx openapi-qraft ... --operation-predefined-parameters \
'/**:header.x-monite-version' \
'get /**,!/auth/**:header.x-monite-entity-id'
yarn exec openapi-qraft ... --operation-predefined-parameters \
'/**:header.x-monite-version' \
'get /**,!/auth/**:header.x-monite-entity-id'
pnpm exec openapi-qraft ... --operation-predefined-parameters \
'/**:header.x-monite-version' \
'get /**,!/auth/**:header.x-monite-entity-id'
With the API client generated, ./api/create-predefined-parameters-request-fn.ts
file will be generated,
and then used to create API Client with predefined parameters.
import { createAPIClient } from './api'; // generated by OpenAPI Qraft CLI
import { createPredefinedParametersRequestFn } from './api/create-predefined-parameters-request-fn.ts'
import { requestFn } from '@openapi-qraft/react';
import { QueryClient } from '@tanstack/react-query';
const requestFnWithPredefinedParameters = createPredefinedParametersRequestFn(
// ๐ Input value will be strictly-typed โจ
[
{
// Qraft CLI identifies ๐ all possible methods even if they are not specified with the ` -- operation-predefined-parameters ` option
requestPattern: 'get,post,patch,delete /**',
parameters: [
{
in: 'header',
name: 'x-monite-version',
value: '2023-06-04' // ๐๏ธ Predefined parameter value
},
],
},
{
requestPattern: 'get /**,!/auth/**', // ๐๏ธ Exclude `/auth/**` endpoints, include only `get` requests
parameters: [
{
in: 'header',
name: 'x-monite-entity-id',
value: async () => { // ๐๏ธ Asynchronous value to be resolved when needed
return 'my-predefined-entity-id';
},
},
],
},
],
requestFn
);
const queryClient = new QueryClient();
// โฌ๏ธ Create a Qraft API client
const api = createAPIClient({
requestFn: requestFnWithPredefinedParameters,
baseUrl: 'https://api.sandbox.monite.com/v1',
queryClient,
});
/**
* ๐ก Requests the `GET /receivables` endpoint with predefined parameters in `header`:
* x-monite-version: 4.4.4
* x-monite-entity-id: my-predefined-entity-id
*/
await api.receivables.getReceivables.fetchQuery();