Skip to content

Commit

Permalink
[fix] Allow Resource to have 'url' member (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Jul 16, 2019
1 parent 1eca188 commit 060be02
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/__tests__/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ export class ArticleResource extends Resource {
}
}

export class UrlArticleResource extends ArticleResource {
readonly url: string = 'happy.com';
}

export class CoolerArticleResource extends ArticleResource {
static urlRoot = 'http://test.com/article-cooler/';
Expand Down
14 changes: 13 additions & 1 deletion src/resource/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,12 @@ export default abstract class Resource {

/** URL to find this Resource */
get url(): string {
if (this.__url !== undefined) return this.__url;
// typescript thinks constructor is just a function
const Static = this.constructor as typeof Resource;
return Static.url(this);
}
private __url?: string;

/** Get the url for a Resource
*
Expand Down Expand Up @@ -237,7 +239,9 @@ export default abstract class Resource {
const getFetchKey = (params: Readonly<object>) => {
return 'GET ' + this.url(params);
};
const schema: SchemaDetail<AbstractInstanceType<T>> = this.getEntitySchema();
const schema: SchemaDetail<
AbstractInstanceType<T>
> = this.getEntitySchema();
const options = this.getRequestOptions();
return {
type: 'read',
Expand Down Expand Up @@ -363,3 +367,11 @@ export default abstract class Resource {
};
}
}

// We're only allowing this to get set for descendants but
// by default we want Typescript to treat it as readonly.
Object.defineProperty(Resource.prototype, 'url', {
set(url: string) {
this.__url = url;
},
});
8 changes: 7 additions & 1 deletion src/resource/__tests__/resource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nock from 'nock';

import { CoolerArticleResource, UserResource } from '../../__tests__/common';
import { CoolerArticleResource, UserResource, UrlArticleResource } from '../../__tests__/common';
import { Resource, normalize } from '..';

describe('Resource', () => {
Expand All @@ -20,6 +20,12 @@ describe('Resource', () => {
it('should not init Resource itself', () => {
expect(() => Resource.fromJS({})).toThrow();
});
it('should work with `url` member', () => {
expect(() => UrlArticleResource.fromJS({})).not.toThrow();
expect(() => UrlArticleResource.fromJS({url: 'five'})).not.toThrow();
const urlArticle = UrlArticleResource.fromJS({url: 'five'});
expect(urlArticle.url).toBe('five');
});
it('should convert class to string', () => {
expect(CoolerArticleResource.toString()).toBeDefined();
expect(CoolerArticleResource.toString()).toMatchInlineSnapshot(
Expand Down

0 comments on commit 060be02

Please sign in to comment.