Returns a promise which resolves to a generated mock response based on the current settings.
If no matching operation could be found in any of the generatedMockApis
, the returned promise will resolve to undefined
.
This allows the consumer to easily implement other fallback logic of their choice.
A function for generating operation mock files for one or more Open API 2.0 definitions. This function expects to be called in a node environment.
At the provided outputPath
, this function will generate a folder for each operation named for its operationId.
Inside that folder, 2 files will be generated:
defaultMockResponse_DONT_EDIT.json
- This file contains a default MockResponseDescription which describes how to generate a mock response for the operation.
This file is generated based on the provided apiDefinition
, but you can customize how it is generated by passing a getDefaultMockRules
function.extendMockResponse.js
- This file contains a ExtendMockResponse function which will be called at runtime when processing a request for this operation.
Feel free to edit this file to provide more specific randomness constraints than what was generated for this operation in the defaultMockResponse_DONT_EDIT.json
file.Programmatically set a mock for the specified operation.
After this function has been called, the provided mock body will override
all existing mock mock body descriptions for this operation until resetMock or resetMocks has been called.
If responseInit
is provided, it will be merged on top of the responseInit from existing mock descriptions until resetMock or resetMocks has been called.
Programmatically set a partial mock for the specified operation. After this function has been called, the provided mock body and responseInit will be deeply merged on top of all existing mock mock body descriptions for this operation until resetMock or resetMocks has been called.
If any mocks have been set for the specified operation by calling mock or partialMock, this function will clear them.
If any mocks have been set by calling mock or partialMock, this function will clear them all.
The default export of the mockIndex.js
file, which is created by the generateDescriptions function.
Settings for a particular mock API, which will affect the behavior of the fetchMock function.
Any requests with URLs starting with this baseUrl
will be mapped to this mock api.
In addition, this baseUrl
will be removed from the request URL before looking up
the mock response in the OpenAPI paths.
You should provide this if you have multiple generated mock APIs.
When the generateDescriptions function is run, it creates mock files for each operation in the apiDefinition
.
The generateDescriptions function also creates a mockIndex.js
file in the API's output directory, which re-exports all of the operation files.
This property should be a reference to the default export of that mockIndex.js
file.
Defaults to true if not provided.
In the process of matching a request to a mock response, the URL of the request must be
compared to the path strings in the OpenAPI definition. If this flag is true
(or not provided),
that path comparison will be case-sensitive, which is how URL matching should work.
However, if you would like, you can set this to false
to make the comparison case-insensitive.
This will also affect how query parameters are mapped to parameter values,
and how the valueOfParameter
MockSchema property is interpreted.
Settings for each mock API that was generated by the generateDescriptions function.
When true, all mock results will be logged using console.log
.
If provided, this function will be called when attempting to construct a mock response to a request that contains a parameter that is invalid according to the OpenAPI definition.
This function will be called at runtime when processing a request for a given API operation.
It receives the operation parameters as an argument and gives you an opportunity to provide more specific randomness constraints
than what was generated for this operation in the defaultMockResponse_DONT_EDIT.json
file.
The return value of this function will be deeply merged with the object in the defaultMockResponse_DONT_EDIT.json
file
before generating the mock response.
The seed used to generate random values. This seed will be a hash of the incoming request object. It is recommended to use this seed if you plan to generate any random values inside the ExtendMockResponse function.
This object describes how to randomly generate a particular mock API response.
It is an intersection of the properties of ResponseInit
and {@link MockResponseBodyDescription}.
An object in JSON Schema format, which describes the structure and constraints for generating a portion of an API response.
For example, if your MockSchema uses the JSON Schema property enum
like this:
{
type: 'string',
enum: ['red', 'green', 'blue']
}
Then the randomly-generated value for that schema will be either 'red', 'green', or 'blue'.
This is primarily accomplished by using the excellent library json-schema-faker
.
You can find all supported JSON Schema properties here.
In addition, @tempworkssoftware/open-api-mocker
supports the special MockSchema properties documented here: {@link MockSchemaSpecialRules}
Either an operationId
or a baseUrl, operationId
pair.
Options passed to the generateDescriptions function.
The OpenAPI 2.0 (Swagger) definition location or structure. If a string, can be a URL for requesting an OpenAPI definition JSON file, or an absolute file path to a local OpenAPI definition JSON file.
An array of file paths relative to the outputPath
for this API specifying additional files
that will be generated when generateDescriptions()
is run, and are therefore safe to delete without warning before new files are generated.
If you use the onGenerateApi
callback to generate custom files for the API,
the user will be warned about deleting those files unless they're included in this array.
An array of file paths relative to an operation directory specifying additional files that
will be generated when generateDescriptions()
is run, and are therefore safe to delete without warning before new files are generated.
If you use the onGenerateOperation
callback to generate custom files within each operation directory,
the user will be warned about deleting those files unless they are included in this array.
Given a partial JSON-schema for an api response, return mock schema properties to add to that schema.
For example, you could check if the schemaName is 'emailAddress' and add a chance
property of 'email'
.
if (schemaName === 'emailAddress') {
return { chance: 'email' }
}
This would mean that, by default, every property named 'emailAddress' returned from this api would be mocked by
the chance.js email
generator.
Note that this function is called recursively for every successful response schema in the api definition.
The schemaFullName
will be a concatenated key path representing the position of that schema in the response.
Array items will be identified by the string '[items]'
. For example, you might see a schemaFullName
like this:
'EmployeesByIdGetResponse.data[items].firstName'
Although this function could be used to customize the response for individual operations, it is designed to be used for broad defaults.
This function will be called after generating files for each API.
It could be used to generate custom files using the fully resolved api definition.
If you do generate custom files here, you will want to pass those fileNames into the
customGeneratedFiles
option to mark them as safe to delete without warning the user before new files are generated.
This function will be called after generating files for each API operation.
It could be used to generate custom operation code,
such as a function for actually making the request or a TypeScript response type.
If you do generate custom operation files here, you will want to pass those fileNames into the
customGeneratedOperationFiles
option to mark them as safe to delete without warning the user before new files are generated.
The absolute path to a directory where the generated code for this apiDefinition will be created.
An array of file paths relative to the outputPath
that should not be deleted when generateDescriptions()
is run.
Defaults to true. Determines if dynamic imports will be used in the generated mockIndex
file, which is the file
that imports each of the individual operation mock files and provides them in a default export.
A representation of an entire apiDefinition
, which will be passed as an argument into the onGenerateApi
function of the ApiGenerationOptions
Generated using TypeDoc
Provide settings to affect the behavior of the fetchMock function.