-
Notifications
You must be signed in to change notification settings - Fork 0
feat(syndesi): introduce client for hypermedia-like JSON/HTTP #238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dherges
wants to merge
5
commits into
master
Choose a base branch
from
syndesi
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fe1f2a8
feat(syndesi): introduce client for hypermedia-like JSON/HTTP
dherges 165647e
docs(syndesi): add user guide
dherges 624b711
style(syndesi): rename to `ApiClient`
dherges e575d11
style: split into single source files
dherges 8601cae
fix(syndesi): try to keep type declaration for resource metadata
dherges File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| - sýndesi / σύνδεση | ||
| - https://en.wiktionary.org/wiki/%CF%83%CF%8D%CE%BD%CE%B4%CE%B5%CF%83%CE%B7 | ||
| - epafí / επαφή | ||
| - https://en.wiktionary.org/wiki/%CE%B5%CF%80%CE%B1%CF%86%CE%AE | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # Syndesi | ||
|
|
||
| > A connected-media format. | ||
|
|
||
| Syndesi is inspired by HAL, Siren, and so on. | ||
|
|
||
| - http://stateless.co/hal_specification.html | ||
| - https://sookocheff.com/post/api/on-choosing-a-hypermedia-format/ | ||
| - https://tools.ietf.org/html/draft-kelly-json-hal-08 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| module.exports = { | ||
| name: 'syndesi', | ||
| preset: '../../jest.config.js', | ||
| coverageDirectory: '../../coverage/libs/syndesi' | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export * from './lib/api-client.service'; | ||
| export * from './lib/call'; | ||
| export * from './lib/embedded.functions'; | ||
| export * from './lib/link.functions'; | ||
| export * from './lib/resource.interfaces'; | ||
| export * from './lib/resource.functions'; | ||
| export * from './lib/uri'; | ||
| export * from './lib/syndesi.module'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { async, TestBed } from '@angular/core/testing'; | ||
| import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; | ||
| import { ApiClient } from './api-client.service'; | ||
| import { Resource, ResourceCollection } from './resource.interfaces'; | ||
|
|
||
| describe('ApiClient', () => { | ||
| beforeEach(async(() => { | ||
| TestBed.configureTestingModule({ | ||
| imports: [ HttpClientTestingModule ], | ||
| providers: [ ApiClient ] | ||
| }).compileComponents(); | ||
| })); | ||
|
|
||
| it('should create', () => { | ||
| const service = TestBed.get(ApiClient); | ||
| expect(service).toBeTruthy(); | ||
| const backend = TestBed.get(HttpTestingController); | ||
| expect(backend).toBeTruthy(); | ||
| }); | ||
|
|
||
| describe(`call()`, () => { | ||
| interface Foo { | ||
| what: string; | ||
| } | ||
|
|
||
| const foo: Resource<Foo> = { | ||
| _links: { | ||
| self: { href: '/foo' } | ||
| }, | ||
| what: 'foo!' | ||
| }; | ||
|
|
||
| interface FooCollectionResource extends ResourceCollection<Foo, any> {} | ||
|
|
||
| it(`should return an Observable`, () => { | ||
| const api: ApiClient = TestBed.get(ApiClient); | ||
| const obs = api.call(foo); | ||
| expect(obs).toBeTruthy(); | ||
| }); | ||
|
|
||
| xit(`should...`, () => { | ||
| const api: ApiClient = TestBed.get(ApiClient); | ||
|
|
||
| api.call(foo).get('next').send<FooCollectionResource>().subscribe(next => { | ||
| const bar = next.resource; | ||
|
|
||
| expect(bar).toBeTruthy(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { Injectable } from '@angular/core'; | ||
| import { HttpClient } from '@angular/common/http'; | ||
| import { Observable } from 'rxjs'; | ||
| import { map } from 'rxjs/operators'; | ||
| import { Call } from './call'; | ||
| import { Resource } from './resource.interfaces'; | ||
|
|
||
| /** | ||
| * Inject `ApiClient` to start brosing a nicely crafted connected-media API. | ||
| * | ||
| * @experimental | ||
| */ | ||
| @Injectable({ providedIn: 'root' }) | ||
| export class ApiClient { | ||
|
|
||
| constructor( | ||
| private http: HttpClient | ||
| ) {} | ||
|
|
||
| /** | ||
| * Get the index page of the API at given `url` | ||
| * | ||
| * @param url URL of index resource, e.g. `/foo/bar/api.json` | ||
| * @return Emits a `Call` object for subsequent API calls | ||
| */ | ||
| public index <T> (url: string): Observable<Call<T>> { | ||
| return this.http.get<Resource<T>> (url).pipe( | ||
| map(res => new Call(this.http, res))); | ||
| } | ||
|
|
||
| /** | ||
| * Get a subsequent call from a resource obtained prior. | ||
| * | ||
| * @param resource | ||
| * @return A `Call` object for subsequent API calls | ||
| */ | ||
| public call <T> (resource: Resource<T>): Call<T> { | ||
| return new Call(this.http, resource); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| import { HttpClient, HttpResponse } from '@angular/common/http'; | ||
| import { Observable } from 'rxjs'; | ||
| import { map } from 'rxjs/operators'; | ||
| import { Resource, ResourceMetadata } from './resource.interfaces'; | ||
| import { expand, UriParams } from './uri'; | ||
|
|
||
| const toNextCall = <S, T>(call: Call<S>) => { | ||
|
|
||
| return function (res: Resource<T> | HttpResponse<Resource<T>>) { | ||
| const json = res instanceof HttpResponse ? res.body : res; | ||
| const response = res instanceof HttpResponse ? res : undefined; | ||
|
|
||
| return new Call<T>(call['_http'], json, response); | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * A `Call` is a single HTTP interaction for exchanging a resource between client and server. | ||
| * | ||
| * ### How To Use | ||
| * | ||
| * Inject `ApiClient` and obtain a call instance from a `Resource` obtained prior to this call: | ||
| * | ||
| * ```ts | ||
| * interface Entity { | ||
| * whatsUp: string; | ||
| * } | ||
| * | ||
| * @Injectable() | ||
| * export class EntityService { | ||
| * constructor(private api: ApiClient) | ||
| * | ||
| * fetchNextPage(res: Respurce<Entity>) { | ||
| * return this.api.call(res).get('next').send<>(); | ||
| * } | ||
| * } | ||
| * ``` | ||
| * | ||
| * @param T Type declaration for a `Resource` obtained prior. | ||
| * @experimental | ||
| */ | ||
| export class Call<T> { | ||
| private _http: HttpClient; | ||
| private _uri: string; | ||
| private _method: string; | ||
| private _options = {}; | ||
|
|
||
| // TODO: public resource: T & ResourceMetadata | ||
| constructor( | ||
| http: HttpClient | Call<any>, | ||
| public resource: Resource<T>, | ||
| public response?: HttpResponse<T> | ||
| ) { | ||
| if (http instanceof HttpClient) { | ||
| this._http = http; | ||
| } else { | ||
| this._http = http._http; | ||
| } | ||
| } | ||
|
|
||
| public delete(rel: string, params?: UriParams, body?: any): Call<T> { | ||
| this.uri(rel, params); | ||
| this.method('delete'); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public get(rel: string, params?: UriParams): Call<T> { | ||
| this.uri(rel, params); | ||
| this.method('get'); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public head(rel: string, params?: UriParams): Call<T> { | ||
| this.uri(rel, params); | ||
| this.method('head'); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public options(rel: string, params?: UriParams): Call<T> { | ||
| this.uri(rel, params); | ||
| this.method('options'); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public patch(rel: string, params?: UriParams): Call<T> { | ||
| this.uri(rel, params); | ||
| this.method('patch'); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public post(rel: string, params?: UriParams, body?: any): Call<T> { | ||
| this.uri(rel, params); | ||
| this.method('post'); | ||
| this.body(body); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public put(rel: string, params?: UriParams, body?: any): Call<T> { | ||
| this.uri(rel, params); | ||
| this.method('put'); | ||
| this.body(body); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public uri(rel: string, params?: UriParams): Call<T> { | ||
| this._uri = this.expandLink(rel, params); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public method(verb: string): Call<T> { | ||
| this._method = verb; | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public opts(opts: any): Call<T> { | ||
| this._options = opts; | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public body(body: any): Call<T> { | ||
| this._options = { | ||
| ...this._options, | ||
| body | ||
| }; | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public send<R>(): Observable<Call<R>> { | ||
| if (!this._method) { | ||
| throw new Error('method is a required parameter! Please set it with method() or one of the short-hands like get().'); | ||
| } | ||
| if (!this._uri) { | ||
| throw new Error('uri is a required parameter! Please set it with uri() or one of the short-hands like get().'); | ||
| } | ||
|
|
||
| return this._http.request<Resource<R>>( | ||
| this._method, | ||
| this._uri, | ||
| { | ||
| ...this._options, | ||
| observe: 'response', | ||
| responseType: 'json' | ||
| } | ||
| ).pipe(map(toNextCall(this))); | ||
| } | ||
|
|
||
| private expandLink(rel: string, params?: UriParams): string { | ||
| const link = this.resource._links[rel]; | ||
|
|
||
| if (!link) { | ||
| throw new Error(`Link with rel=${rel} does not exist`); | ||
| } | ||
|
|
||
| if (link instanceof Array) { | ||
| throw new Error('Traversing arrays not implemented'); | ||
| } else { | ||
| return expand(link, params); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a weak link in the
Callapi. From<R>send(), you always get aCall<R>with aResource<R>. What aboutCall<R, D>to get aR extends Resource<D>?I think we need:
call()w/o generics to get an any-typedResource<any><R>call()w/ generic type to get anR extends Resource<?>Thus, the
Call<R extends Resource<?>>call class would work with Resource....There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In typescript playground, it's supposed to work in TypeScript 3.3