Skip to content

Commit 676aba3

Browse files
Resolve function and general housekeeping (#30)
1 parent 85b3e24 commit 676aba3

10 files changed

+107
-153
lines changed

.github/workflows/npm-publish.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- name: Set up Node.js
1414
uses: actions/setup-node@v3
1515
with:
16-
node-version: 16
16+
node-version: 18
1717
registry-url: https://registry.npmjs.org/
1818
- name: Set version from tag
1919
run: npm version ${{ github.event.release.tag_name }} --no-git-tag-version

.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v18

CHANGELOG.md

+15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.8.0]
8+
9+
### Added
10+
11+
- `resolve` function for resolving a `SubEntity`
12+
- [Development section in the README](./README.md#development)
13+
14+
### Changed
15+
16+
- Project is now built with Node 18
17+
18+
### Removed
19+
20+
- Contribution guidelines and code of conduct, which now live [here](https://github.com/siren-js/.github/tree/main/profile)
21+
722
## [0.7.0] - 2023-04-20
823

924
> **NOTE:** This release includes breaking changes from [v0.6.0](#060---2023-03-06).

CODE_OF_CONDUCT.md

-79
This file was deleted.

CONTRIBUTING.md

-69
This file was deleted.

README.md

+36-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Build Workflow](https://img.shields.io/github/actions/workflow/status/siren-js/client/build.yaml?style=flat-square)]()
55
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
66
[![License](https://img.shields.io/github/license/siren-js/client?style=flat-square)](LICENSE)
7-
[![Contributing](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat-square)](CONTRIBUTING.md)
7+
[![Contributing](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat-square)](https://github.com/siren-js/.github/blob/main/profile/CONTRIBUTING.md)
88

99
[Siren](https://github.com/kevinswiber/siren) API client library for JavaScript
1010

@@ -23,6 +23,7 @@ Siren is a very powerful hypermedia format that enables a server and its clients
2323
- [Background](#background)
2424
- [Install](#install)
2525
- [Usage](#usage)
26+
- [Development](#development)
2627
- [API](#api)
2728
- [Maintainer](#maintainer)
2829
- [Contributing](#contributing)
@@ -77,6 +78,39 @@ if (editAction != null) {
7778
}
7879
```
7980

81+
## Development
82+
83+
```sh
84+
# setup Node.js
85+
$ nvm use
86+
87+
# test with Jest
88+
$ npm test
89+
# run tests in watch mode
90+
$ npm run test:watch
91+
# run tests with coverage
92+
$ npm run test:cov
93+
94+
# compile TypeScript code
95+
$ npm run compile
96+
97+
# lint with ESLint
98+
$ npm run lint
99+
# automatically fix lint issues where possible
100+
$ npm run lint:fix
101+
102+
# format files with Prettier
103+
$ npm run format
104+
# check files for proper formatting
105+
$ npm run format:check
106+
107+
# build the library (compile, lint, format check)
108+
$ npm run build:lib
109+
110+
# generate docs with TypeDoc
111+
$ npm run build:docs
112+
```
113+
80114
## API
81115

82116
See our [docs](https://siren-js.github.io/client).
@@ -87,7 +121,7 @@ See our [docs](https://siren-js.github.io/client).
87121

88122
## Contributing
89123

90-
See our [contribution guidelines](./CONTRIBUTING.md).
124+
See our [contribution guidelines](https://github.com/siren-js/.github/blob/main/profile/CONTRIBUTING.md).
91125

92126
PRs accepted.
93127

_config.yml

-1
This file was deleted.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@siren-js/client",
33
"version": "0.0.0",
4-
"description": "Client class for communicating with Siren APIs",
4+
"description": "Siren API client library",
55
"files": [
66
"/dist"
77
],
@@ -27,6 +27,7 @@
2727
},
2828
"keywords": [
2929
"API",
30+
"client",
3031
"HATEOAS",
3132
"hypermedia",
3233
"REST",

src/resolve.spec.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import nock from 'nock';
2+
3+
import { entity, siren } from '../test/stubs';
4+
import { EmbeddedEntity, EmbeddedLink } from './';
5+
import { resolve } from './resolve';
6+
7+
describe('resolve', () => {
8+
beforeEach(() => {
9+
if (!nock.isActive()) {
10+
nock.activate();
11+
}
12+
});
13+
14+
afterEach(() => {
15+
nock.restore();
16+
});
17+
18+
it('should return EmbeddedEntity', async () => {
19+
const subEntity = new EmbeddedEntity();
20+
21+
const result = await resolve(subEntity);
22+
23+
expect(result).toBe(subEntity);
24+
});
25+
26+
it('should follow and parse EmbeddedLink', async () => {
27+
const url = 'https://api.example.com/foo';
28+
const subEntity = new EmbeddedLink();
29+
subEntity.href = url;
30+
const scope = nock(url).get('').reply(200, siren);
31+
32+
const result = await resolve(subEntity);
33+
34+
expect(result).toStrictEqual(entity);
35+
expect(scope.isDone()).toBe(true);
36+
});
37+
});

src/resolve.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { follow } from './follow';
2+
import { EmbeddedLink, Entity, SubEntity } from './models';
3+
import { parse } from './parse';
4+
5+
/**
6+
* Resolves a {@linkcode SubEntity} to an {@linkcode Entity}. An
7+
* `EmbeddedEntity` is returned as is, while an {@linkcode EmbeddedLink} is
8+
* {@linkcode follow | followed} and {@linkcode parse | parsed}.
9+
*/
10+
export async function resolve<T extends object = object>(subEntity: SubEntity): Promise<Entity<T>> {
11+
if (subEntity instanceof EmbeddedLink) {
12+
return follow(subEntity).then(parse) as Promise<Entity<T>>;
13+
}
14+
return Promise.resolve(subEntity as unknown as Entity<T>);
15+
}

0 commit comments

Comments
 (0)