From f679753b1a514f7539991021a582519c55ee0837 Mon Sep 17 00:00:00 2001 From: wildhaber Date: Sun, 28 Oct 2018 18:09:13 +0100 Subject: [PATCH 1/2] use IRI as resource path when given --- src/model.ts | 9 +++++++-- test/unit/model.test.ts | 12 ++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/model.ts b/src/model.ts index b4d1419..736fe41 100644 --- a/src/model.ts +++ b/src/model.ts @@ -685,9 +685,14 @@ export class JSORMBase { } static url(id?: string | number): string { - const endpoint = this.endpoint || `/${this.jsonapiType}` + const isIri = (id && typeof id === 'string' && id.startsWith('/')) + const endpoint = (isIri) ? id : (this.endpoint || `/${this.jsonapiType}`) let base = `${this.fullBasePath()}${endpoint}` + if (isIri) { + return base; + } + if (id) { base = `${base}/${id}` } @@ -941,4 +946,4 @@ export const isModelInstance = (arg: any): arg is JSORMBase => { return false } return isModelClass(arg.constructor.currentClass) -} \ No newline at end of file +} diff --git a/test/unit/model.test.ts b/test/unit/model.test.ts index a4a2a54..46708eb 100644 --- a/test/unit/model.test.ts +++ b/test/unit/model.test.ts @@ -1391,6 +1391,18 @@ describe("Model", () => { "http://base.com/namespace/v1/testtype/testId" ) }) + + it("should use IRI as resource path", () => { + class DefaultBaseUrl extends ApplicationRecord { + static baseUrl: string = "http://base.com" + static apiNamespace: string = "/namespace/v1" + static jsonapiType: string = "testtype" + } + + expect(DefaultBaseUrl.url("/testtype/testId")).to.eq( + "http://base.com/namespace/v1/testtype/testId" + ) + }) }) context("Base URL path generation is overridden", () => { From 334f86f0d2918bfdbebc543849bc79d76e5ee62e Mon Sep 17 00:00:00 2001 From: wildhaber Date: Sun, 28 Oct 2018 18:16:36 +0100 Subject: [PATCH 2/2] prevent newline at end of file --- src/model.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.ts b/src/model.ts index 736fe41..f52f468 100644 --- a/src/model.ts +++ b/src/model.ts @@ -946,4 +946,4 @@ export const isModelInstance = (arg: any): arg is JSORMBase => { return false } return isModelClass(arg.constructor.currentClass) -} +} \ No newline at end of file