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

[fix] Allow Resource to have 'url' member #107

Merged
merged 1 commit into from
Jul 16, 2019
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
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;
},
});
6 changes: 5 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,10 @@ 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();
});
it('should convert class to string', () => {
expect(CoolerArticleResource.toString()).toBeDefined();
expect(CoolerArticleResource.toString()).toMatchInlineSnapshot(
Expand Down