Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tynarus/apily
A basic mock service written in JavaScript, for easy implementation into JavaScript UI projects requiring mocks for development or testing.
https://github.com/tynarus/apily
apily expressjs http-mock http-mocking http-request http-response javascript mock mock-api mock-data mock-framework mock-functions mock-library mock-server mock-service mocking mocking-framework mocks node typescript
Last synced: 12 days ago
JSON representation
A basic mock service written in JavaScript, for easy implementation into JavaScript UI projects requiring mocks for development or testing.
- Host: GitHub
- URL: https://github.com/tynarus/apily
- Owner: Tynarus
- License: gpl-3.0
- Created: 2019-09-13T18:16:38.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-03-01T22:31:31.000Z (over 1 year ago)
- Last Synced: 2024-10-29T05:48:10.128Z (22 days ago)
- Topics: apily, expressjs, http-mock, http-mocking, http-request, http-response, javascript, mock, mock-api, mock-data, mock-framework, mock-functions, mock-library, mock-server, mock-service, mocking, mocking-framework, mocks, node, typescript
- Language: TypeScript
- Homepage:
- Size: 59.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Apily
A basic mock service written in JavaScript, for easy implementation into JavaScript UI projects requiring mocks for development or testing.
Apily is written in TypeScript, and as such includes typings. Documentation examples will be written in TypeScript.
## Setting up a Mock Service
Once the Apily package is installed, standing a mock service up is fairly straightforward. We'll start by making the main file that will launch the mock server.
For TypeScript projects, create a new directory called `mock/` at the root of your project, with an appropriate `tsconfig.json` file (your desired tsconfig may vary):
```json
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "es6",
"outDir": "./dist"
},
"include": [
"./**/*.ts"
],
"exclude": [
"../node_modules"
]
}
```
We'll also be adding a new file named `mock-server.ts` with the following content:```typescript
import { start } from 'apily';const port = 4300;
start(port);
```From there, we can add a new script to our `package.json`:
```json
"scripts": {
"mock-server": "tsc --project ./mock/tsconfig.json && node ./mock/dist/mock/mock-server.js"
}
```Running this script with `npm run mock-server` should result in the mock service starting with the message `Loaded 0 mock requests`. The port specified above was `4300`, so we should now be able to hit our empty mock service at `http://localhost:4300`.
## Creating Mocks
Heading back into `mock-server.ts`, we can create our very first mock endpoint:
```typescript
import { start, mock } from 'apily';mock({
method: 'GET',
url: '/test',
responseStatus: 200,
responseBody: {
text: 'Hello world!'
}
});start();
```Restart the mock service, and instead we should now get the message `Loaded 1 mock requests`. If we try to hit `http://localhost:4300/test` in our browser, we should be given a JSON response of `{ text: 'Hello world!' }`
Ideally as more mock are created, they'll be extrapolated out into their own mock files. We can move this test mock into a new file named `test-mock.ts`:
```typescript
import { mock } from 'apily';mock({
method: 'GET',
url: '/test',
responseStatus: 200,
responseBody: {
text: 'Hello world!'
}
});
```...and instead now import that file into the main `mock-server.ts` file:
```typescript
import { start } from 'apily';import './test-mock';
start();
```This keeps our main mock service file clean and makes mocks easier to find.
## Mock Config
The `mock()` function takes a single parameter, a `MockOptions` object. `MockOptions` is declared as follows:
```typescript
interface MockOptions {
priority?: number;
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
url: string;
requestHeaders?: any;
requestParams?: any;
requestBody?: any;
responseStatus: number;
responseHeaders?: any;
responseBody?: any;
}
```- `priority` is the priority of the mock being created. In the case that multiple mocks are found for a given request, the one with the lowest `priority` value will be used. If multiple are found with the same `priority` value, the first one in wins.
- `method` is the required request method. `GET`, `POST`, `PUT`, `PATCH`, or `DELETE`. All calls with the `OPTIONS` method are ignored by Apily.
- `url` the exact url for the mock. `/test`, `/users/1/details`, `/news/items`, or whatever your particular request URL may be. Regex param variables are planned for a future release.
- `requestHeaders` the headers that the mock requires. For example, setting this to `{ 'Authorization': 'Bearer mytoken' }` will mean that the mock will require that any requests send the `Authorization` header with the value `Bearer mytoken`.
- `requestParams` the URL params that the mock requires. This is not yet functional and is planned for a future release.
- `requestBody` the JSON request body required by the mock.
- `responseStatus` the response status code. Ie `200` for OK, `401` for Unauthorized, `500` for internal server error, etc.
- `responseHeaders` the response headers that will be returned by the mock.
- `responseBody` the body that will be returned by the mock. Accepts strings, JSON, or a `ResponseFile` object that accepts a `fileName` parameter.
- It is possible to return static files, ie PDFs, as such: Supply a `responseBody` value of `new ResponseFile(myFileNameAndPath)` with a matching `responseHeaders` header of `'content-type': 'application/pdf'`.