Skip to main content
Version: 2.x ๐Ÿšง

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โ€‹

  1. inputPredefinedParameters: InputPredefinedParametersItem[] - Required predefined parameters for the operations
    • inputPredefinedParameters.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 operation
      • inputPredefinedParameters.parameters.in - Required parameter location
      • inputPredefinedParameters.parameters.name - Required parameter name
      • inputPredefinedParameters.parameters.value: T | (() => Promise<T>) - Required parameter value
  2. baseRequestFn - Required requestFn 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:

npx 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();