https://github.com/curveball/links
Weblinking middelware for Curveball
https://github.com/curveball/links
curveball hal hateoas http links middleware rest weblinking
Last synced: 10 months ago
JSON representation
Weblinking middelware for Curveball
- Host: GitHub
- URL: https://github.com/curveball/links
- Owner: curveball
- License: mit
- Created: 2020-03-22T05:35:59.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2024-11-11T21:26:27.000Z (over 1 year ago)
- Last Synced: 2025-05-23T13:38:23.800Z (11 months ago)
- Topics: curveball, hal, hateoas, http, links, middleware, rest, weblinking
- Language: TypeScript
- Size: 780 KB
- Stars: 0
- Watchers: 8
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: changelog.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
Curveball Links middleware
==========================
This [Curveball][1] middleware helps you parse and generate HTTP Weblinks.
Any links passed in a request via the [`Link`][3] header will now be exposed in
`ctx.request`. Similarly, if a link was set in `ctx.response.links`, it will
automatically be encoded as a `Link` header in the HTTP response.
Lastly, if the request body was a [HAL][2] object, and it has a `_links`
property, these links will also be added to `ctx.response.links`.
Installation
------------
npm i @curveball/links
Usage
-----
```typescript
import { Application, Context } from '@curveball/core';
import links from '@curveball/links';
import bodyParser from '@curveball/bodyparser';
const app = new Application();
// If you would like to parse HAL links, the bodyParser must be loaded first.
app.use(bodyParser());
app.use(links());
app.use( (ctx: Context) => {
// Read a Link from a HAL body or Link header
console.log(ctx.request.links.get('author'));
// Write a Link header
ctx.response.links.set({ rel: 'alternate' , type: 'text/csv', href: '/export.csv'});
});
```
[1]: https://curveballjs.org/
[2]: https://tools.ietf.org/html/draft-kelly-json-hal-08
[3]: https://tools.ietf.org/html/rfc8288