Skip to content

Commit

Permalink
feat(data): add ability to configure trailing slashes (#3357)
Browse files Browse the repository at this point in the history
  • Loading branch information
santoshyadavdev authored Mar 28, 2022
1 parent 9560186 commit 56aedfd
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
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

0 comments on commit 56aedfd

Please sign in to comment.