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

feat(data): add ability to configure trailing slashes #3357

Merged
merged 1 commit into from
Mar 28, 2022
Merged
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
22 changes: 22 additions & 0 deletions modules/data/spec/dataservices/default-data.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,5 +543,27 @@ describe('DefaultDataServiceFactory', () => {
heroDS.getAll();
expect(http.get).toHaveBeenCalledWith(newHeroesUrl, undefined);
});

it('should keep trailing slash', () => {
const newHeroesUrl = 'some/other/api/heroes/';
const config: DefaultDataServiceConfig = {
root: '//example.com/api/',
entityHttpResourceUrls: {
Hero: {
entityResourceUrl: '/api/hero/',
collectionResourceUrl: newHeroesUrl,
},
},
trailingSlashEndpoints: true,
};
const factory = new DefaultDataServiceFactory(
http,
httpUrlGenerator,
config
);
const heroDS = factory.create<Hero>('Hero');
heroDS.getAll();
expect(http.get).toHaveBeenCalledWith(newHeroesUrl, undefined);
});
});
});
2 changes: 2 additions & 0 deletions modules/data/src/dataservices/default-data-service-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ export abstract class DefaultDataServiceConfig {
saveDelay?: number;
/** request timeout in MS (default: 0)*/
timeout?: number; //
/** to keep trailing slashes or not; false by default */
trailingSlashEndpoints?: boolean;
}
5 changes: 4 additions & 1 deletion modules/data/src/dataservices/default-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class DefaultDataService<T> implements EntityCollectionDataService<T> {
protected getDelay = 0;
protected saveDelay = 0;
protected timeout = 0;
protected trailingSlashEndpoints = false;

get name() {
return this._name;
Expand All @@ -53,9 +54,11 @@ export class DefaultDataService<T> implements EntityCollectionDataService<T> {
getDelay = 0,
saveDelay = 0,
timeout: to = 0,
trailingSlashEndpoints = false,
} = config || {};
this.delete404OK = delete404OK;
this.entityUrl = httpUrlGenerator.entityResource(entityName, root);
this.entityUrl = httpUrlGenerator.entityResource(entityName, root,
trailingSlashEndpoints);
this.entitiesUrl = httpUrlGenerator.collectionResource(entityName, root);
this.getDelay = getDelay;
this.saveDelay = saveDelay;
Expand Down
13 changes: 8 additions & 5 deletions modules/data/src/dataservices/http-url-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export abstract class HttpUrlGenerator {
* Return the base URL for a single entity resource,
* e.g., the base URL to get a single hero by its id
*/
abstract entityResource(entityName: string, root: string): string;
abstract entityResource(entityName: string, root: string,
trailingSlashEndpoints: boolean): string;

/**
* Return the base URL for a collection resource,
Expand Down Expand Up @@ -77,11 +78,12 @@ export class DefaultHttpUrlGenerator implements HttpUrlGenerator {
*/
protected getResourceUrls(
entityName: string,
root: string
root: string,
trailingSlashEndpoints: boolean = false
): HttpResourceUrls {
let resourceUrls = this.knownHttpResourceUrls[entityName];
if (!resourceUrls) {
const nRoot = normalizeRoot(root);
const nRoot = trailingSlashEndpoints ? root: normalizeRoot(root);
resourceUrls = {
entityResourceUrl: `${nRoot}/${entityName}/`.toLowerCase(),
collectionResourceUrl: `${nRoot}/${this.pluralizer.pluralize(
Expand All @@ -99,8 +101,9 @@ export class DefaultHttpUrlGenerator implements HttpUrlGenerator {
* @param root {string} Root path to the resource, e.g., 'some-api`
* @returns complete path to resource, e.g, 'some-api/hero'
*/
entityResource(entityName: string, root: string): string {
return this.getResourceUrls(entityName, root).entityResourceUrl;
entityResource(entityName: string, root: string,
trailingSlashEndpoints: boolean): string {
return this.getResourceUrls(entityName, root, trailingSlashEndpoints).entityResourceUrl;
}

/**
Expand Down