Skip to content

Commit 56aedfd

Browse files
feat(data): add ability to configure trailing slashes (#3357)
1 parent 9560186 commit 56aedfd

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

modules/data/spec/dataservices/default-data.service.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,5 +543,27 @@ describe('DefaultDataServiceFactory', () => {
543543
heroDS.getAll();
544544
expect(http.get).toHaveBeenCalledWith(newHeroesUrl, undefined);
545545
});
546+
547+
it('should keep trailing slash', () => {
548+
const newHeroesUrl = 'some/other/api/heroes/';
549+
const config: DefaultDataServiceConfig = {
550+
root: '//example.com/api/',
551+
entityHttpResourceUrls: {
552+
Hero: {
553+
entityResourceUrl: '/api/hero/',
554+
collectionResourceUrl: newHeroesUrl,
555+
},
556+
},
557+
trailingSlashEndpoints: true,
558+
};
559+
const factory = new DefaultDataServiceFactory(
560+
http,
561+
httpUrlGenerator,
562+
config
563+
);
564+
const heroDS = factory.create<Hero>('Hero');
565+
heroDS.getAll();
566+
expect(http.get).toHaveBeenCalledWith(newHeroesUrl, undefined);
567+
});
546568
});
547569
});

modules/data/src/dataservices/default-data-service-config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ export abstract class DefaultDataServiceConfig {
2323
saveDelay?: number;
2424
/** request timeout in MS (default: 0)*/
2525
timeout?: number; //
26+
/** to keep trailing slashes or not; false by default */
27+
trailingSlashEndpoints?: boolean;
2628
}

modules/data/src/dataservices/default-data.service.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export class DefaultDataService<T> implements EntityCollectionDataService<T> {
3434
protected getDelay = 0;
3535
protected saveDelay = 0;
3636
protected timeout = 0;
37+
protected trailingSlashEndpoints = false;
3738

3839
get name() {
3940
return this._name;
@@ -53,9 +54,11 @@ export class DefaultDataService<T> implements EntityCollectionDataService<T> {
5354
getDelay = 0,
5455
saveDelay = 0,
5556
timeout: to = 0,
57+
trailingSlashEndpoints = false,
5658
} = config || {};
5759
this.delete404OK = delete404OK;
58-
this.entityUrl = httpUrlGenerator.entityResource(entityName, root);
60+
this.entityUrl = httpUrlGenerator.entityResource(entityName, root,
61+
trailingSlashEndpoints);
5962
this.entitiesUrl = httpUrlGenerator.collectionResource(entityName, root);
6063
this.getDelay = getDelay;
6164
this.saveDelay = saveDelay;

modules/data/src/dataservices/http-url-generator.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export abstract class HttpUrlGenerator {
4141
* Return the base URL for a single entity resource,
4242
* e.g., the base URL to get a single hero by its id
4343
*/
44-
abstract entityResource(entityName: string, root: string): string;
44+
abstract entityResource(entityName: string, root: string,
45+
trailingSlashEndpoints: boolean): string;
4546

4647
/**
4748
* Return the base URL for a collection resource,
@@ -77,11 +78,12 @@ export class DefaultHttpUrlGenerator implements HttpUrlGenerator {
7778
*/
7879
protected getResourceUrls(
7980
entityName: string,
80-
root: string
81+
root: string,
82+
trailingSlashEndpoints: boolean = false
8183
): HttpResourceUrls {
8284
let resourceUrls = this.knownHttpResourceUrls[entityName];
8385
if (!resourceUrls) {
84-
const nRoot = normalizeRoot(root);
86+
const nRoot = trailingSlashEndpoints ? root: normalizeRoot(root);
8587
resourceUrls = {
8688
entityResourceUrl: `${nRoot}/${entityName}/`.toLowerCase(),
8789
collectionResourceUrl: `${nRoot}/${this.pluralizer.pluralize(
@@ -99,8 +101,9 @@ export class DefaultHttpUrlGenerator implements HttpUrlGenerator {
99101
* @param root {string} Root path to the resource, e.g., 'some-api`
100102
* @returns complete path to resource, e.g, 'some-api/hero'
101103
*/
102-
entityResource(entityName: string, root: string): string {
103-
return this.getResourceUrls(entityName, root).entityResourceUrl;
104+
entityResource(entityName: string, root: string,
105+
trailingSlashEndpoints: boolean): string {
106+
return this.getResourceUrls(entityName, root, trailingSlashEndpoints).entityResourceUrl;
104107
}
105108

106109
/**

0 commit comments

Comments
 (0)