https://github.com/launchcodedev/api-fields-middleware
API Fields, as composable koa middleware
https://github.com/launchcodedev/api-fields-middleware
Last synced: 11 months ago
JSON representation
API Fields, as composable koa middleware
- Host: GitHub
- URL: https://github.com/launchcodedev/api-fields-middleware
- Owner: launchcodedev
- License: mpl-2.0
- Created: 2020-06-12T15:49:02.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-24T02:54:04.000Z (about 3 years ago)
- Last Synced: 2025-01-07T17:42:56.145Z (about 1 year ago)
- Language: TypeScript
- Size: 1.28 MB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# API Fields Middlware
[](https://www.mozilla.org/en-US/MPL/2.0/)
[](https://github.com/launchcodedev/api-fields-middleware/actions)
[](https://www.npmjs.com/package/@lcdev/api-fields-middleware)
Use API Fields easily in Koa.
```bash
yarn add @lcdev/api-fields-middleware@0.1
```
```typescript
import { ApiField } from '@lcdev/api-fields';
// somewhere, where your model is defined
class EntityA {
@ApiField()
publicFieldA: any;
@ApiField()
publicFieldB: any;
privateFieldA: any;
privateFieldB: any;
}
import { extractApiFieldsMiddleware } from '@lcdev/api-fields-middleware';
// somewhere, where you have a koa router
app
.get('/foo/bar', extractApiFieldsMiddleware(EntityA), async (ctx, next) => {
// the body ends up as an object that is, or looks like an 'EntityA'
ctx.body = new EntityA();
return next();
});
```
This will perform the `extract` with `ApiFields` of `EntityA`, after your router
middleware has run. This will only apply **if the response is a 200**.
Features:
- Supports `extractApiFieldsMiddleware([EntityA])` to imply that the api response is an array of EntityA
- Supports `extractApiFieldsMiddleware({ ... }, true)` to pass a "raw" `Extraction` object to use (you might use `getApiFields` with an extension here)