Skip to content
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

Add a 'submitFollow' method to 'Action' #484

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 55 additions & 11 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { State } from './state';
import * as qs from 'querystring';
import Client from './client';
import { Field } from './field';
import Resource from './resource';

export interface ActionInfo {

Expand Down Expand Up @@ -51,6 +52,16 @@ export interface Action<T extends Record<string, any> = Record<string, any>> ext
*/
submit(formData: T): Promise<State>;

/**
* Execute the action or submit the form, then return the next resource.
*
* If a server responds with a 201 Status code and a Location header,
* it will automatically return the newly created resource.
*
* If the server responded with a 204 or 205, this function will return
* `this`.
*/
submitFollow(formData: T): Promise<Resource>;
}

export class SimpleAction<TFormData extends Record<string, any>> implements Action {
Expand Down Expand Up @@ -110,6 +121,44 @@ export class SimpleAction<TFormData extends Record<string, any>> implements Acti

const uri = new URL(this.uri);

const newFormData = this.validateForm(formData);

if (this.method === 'GET') {
uri.search = qs.stringify(newFormData);
const resource = this.client.go(uri.toString());
return resource.get();
}
const response = await this.fetchOrThrowWithBody(uri, newFormData);
const state = this.client.getStateForResponse(uri.toString(), response);
return state;
}

async submitFollow(formData: TFormData): Promise<Resource> {
const uri = new URL(this.uri);

const newFormData = this.validateForm(formData);

if (this.method === 'GET') {
uri.search = qs.stringify(newFormData);
return this.client.go(uri.toString());
}

const response = await this.fetchOrThrowWithBody(uri, newFormData);
switch (response.status) {
case 201:
if (response.headers.has('location')) {
return this.client.go(response.headers.get('location')!);
}
throw new Error('Could not follow after a 201 request, because the server did not reply with a Location header. If you sent a Location header, check if your service is returning "Access-Control-Expose-Headers: Location".');
case 204 :
case 205 :
return this.client.go(uri.toString());
default:
throw new Error('Did not receive a 201, 204 or 205 status code so we could not follow to the next resource');
}
}

private validateForm(formData: TFormData): TFormData {
const newFormData: TFormData = {
...formData
};
Expand All @@ -129,33 +178,28 @@ export class SimpleAction<TFormData extends Record<string, any>> implements Acti
}

}
return newFormData;
}

if (this.method === 'GET') {
uri.search = qs.stringify(newFormData);
const resource = this.client.go(uri.toString());
return resource.get();
}
private fetchOrThrowWithBody(uri: URL, formData: TFormData): Promise<Response> {
let body;
switch (this.contentType) {
case 'application/x-www-form-urlencoded' :
body = qs.stringify(newFormData);
body = qs.stringify(formData);
break;
case 'application/json':
body = JSON.stringify(newFormData);
body = JSON.stringify(formData);
break;
default :
throw new Error(`Serializing mimetype ${this.contentType} is not yet supported in actions`);
}
const response = await this.client.fetcher.fetchOrThrow(uri.toString(), {
return this.client.fetcher.fetchOrThrow(uri.toString(), {
method: this.method,
body,
headers: {
'Content-Type': this.contentType
}
});
const state = this.client.getStateForResponse(uri.toString(), response);
return state;

}
}

Expand Down
2 changes: 1 addition & 1 deletion test/unit/state/hal-forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { factory } from '../../../src/state/hal';
import { Action, Client, Field } from '../../../src';
import { HalFormsProperty } from 'hal-types';

type CompareAction = Omit<Action, 'submit'>;
type CompareAction = Omit<Action, 'submit' | 'submitFollow'>;

describe('HAL forms', () => {

Expand Down
Loading