Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
aspida-mock
Advanced tools
Readme
TypeScript friendly RESTful API mock for aspida.
GET
/POST
/PUT
/DELETE
in a few lines.Using npm:
$ npm install aspida-mock @aspida/axios axios
# $ npm install aspida-mock @aspida/fetch
# $ npm install aspida-mock @aspida/node-fetch node-fetch
# $ npm install aspida-mock @aspida/ky ky
Using Yarn:
$ yarn add aspida-mock @aspida/axios axios
# $ yarn add aspida-mock @aspida/fetch
# $ yarn add aspida-mock @aspida/node-fetch node-fetch
# $ yarn add aspida-mock @aspida/ky ky
Export mockMethods in aspida type definition file.
api/users/index.ts
import { mockMethods } from "aspida-mock";
export type Methods = {
post: {
query: { id: number };
reqHeaders: { val: string };
reqBody: { name: string };
resHeaders: { token: string };
resBody: {
id: number;
name: string;
};
};
};
export default mockMethods<Methods>({
post: ({ query, reqHeaders, reqBody }) => ({
status: 200,
resHeaders: { token: reqHeaders.val },
resBody: {
id: query.id,
name: reqBody.name,
},
}),
});
package.json
{
"scripts": {
"build": "aspida && aspida-mock"
}
}
tarminal
$ npm run build
index.ts
import aspidaClient from "@aspida/axios"; // "@aspida/fetch", "@aspida/node-fetch", "@aspida/ky"
import api from "./api/$api";
import mock from "./api/$mock";
const client = process.env.NODE_ENV === "development" ? mock(aspidaClient()) : api(aspidaClient());
(async () => {
const res = await client.users.post({
query: { id: 0 },
headers: { val: "hoge" },
data: { name: "fuga" },
});
console.log(res);
/*
{
status: 200,
headers: { token: "hoge" },
data: { id: 0, name: "fuga" }
}
*/
})();
For every request, you can insert processing before reaching mockMethods.
api/@middleware.ts
import { mockMiddleware } from "aspida-mock";
export default mockMiddleware([
(req, _res, next) => {
next({ ...req, query: { hoge: req.query.hoge + 1 } });
},
(req, res) => {
res({ status: 200, resBody: { fuga: req.query.hoge + 2 } });
},
]);
api/users/index.ts
import { mockMethods } from "aspida-mock";
export type Methods = {
get: {
query: { hoge: number };
resBody: {
fuga: number;
};
};
};
export default mockMethods<Methods>({
get: ({ query }) => ({
status: 200,
resBody: { fuga: query.hoge + 4 },
}),
});
index.ts
import aspidaClient from "@aspida/axios"; // "@aspida/fetch", "@aspida/node-fetch", "@aspida/ky"
import mock from "./api/$mock";
const client = mock(aspidaClient());
(async () => {
const res = await client.users.get({
query: { hoge: 0 },
});
console.log(res);
/*
{
status: 200,
data: { fuga: 3 }
}
*/
})();
aspida-mock has several options available.
delayMSec: number
Simulate response delay.
import aspidaClient from "@aspida/axios"; // "@aspida/fetch", "@aspida/node-fetch", "@aspida/ky"
import mock from "./api/$mock";
const client = mock(aspidaClient(), { delayMSec: 500 });
(async () => {
console.time();
await client.users.$get();
console.timeEnd(); // default: 506.590ms
})();
log: boolean
Switch request log output.
import aspidaClient from "@aspida/axios"; // "@aspida/fetch", "@aspida/node-fetch", "@aspida/ky"
import mock from "./api/$mock";
const client = mock(aspidaClient(), { log: true });
(async () => {
await client.users.$get({ query: { bar: "baz" } });
// [mock] get: /users?bar=baz => 200
})();
.gitignore
Exclude $mock.ts
generated by aspida-mock in the build from Git monitoring.
$ echo "\$mock.ts" >> .gitignore
# If Windows (Command Prompt)
> echo $mock.ts >> .gitignore
The following options can be specified in the Command Line Interface.
Option | Type | Default | Description |
---|---|---|---|
--config -c | string | "aspida.config.js" | Specify the path to the configuration file. |
--watch -w |
Enable watch mode. Regenerate $mock.ts according to
the increase / decrease of the API endpoint file.
| ||
--version -v | Print aspida-mock version. |
aspida-mock refers to only "input" among the items of the aspida configuration file aspida.config.js
.
This allows you to always generate a mock from the same directory as aspida.
Options of aspida.config.js
aspida-mock is licensed under a MIT License.
FAQs
TypeScript friendly RESTful API mock for aspida
The npm package aspida-mock receives a total of 468 weekly downloads. As such, aspida-mock popularity was classified as not popular.
We found that aspida-mock demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.