Skip to content

Commit

Permalink
Jsonapi class detection (#1153)
Browse files Browse the repository at this point in the history
* Remove the unused code

* Add jsonapi class detection

* Document the new util function
  • Loading branch information
DarkoKukovec authored Mar 7, 2023
1 parent 824bf84 commit 159158f
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 20 deletions.
8 changes: 8 additions & 0 deletions docs/jsonapi/jsonapi-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,11 @@ clearCacheByType(type: IType);
```

Clear the network cache for the given model type.

### isJsonApiClass

```typescript
function isJsonApiClass(type: typeof PureModel | typeof Collection | typeof View): boolean;
```

Check if the given type is a JSON API class (was decorated with `jsonapi()`).
2 changes: 2 additions & 0 deletions packages/datx-jsonapi/src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export const MODEL_RELATED_FIELD = 'jsonapiRelated';

// eslint-disable-next-line no-useless-escape
export const URL_REGEX = /^((https?\:)?\/\/)/;

export const DATX_JSONAPI_CLASS = Symbol('@datx/jsonapi class');
3 changes: 3 additions & 0 deletions packages/datx-jsonapi/src/decorateCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { libFetch, read } from './NetworkUtils';
import { Response } from './Response';
import { CachingStrategy } from '@datx/network';
import { IGetAllResponse } from './interfaces/IGetAllResponse';
import { DATX_JSONAPI_CLASS } from './consts';

type TSerialisedStore = IRawCollection & { cache?: Array<Omit<ICacheInternal, 'collection'>> };

Expand Down Expand Up @@ -66,6 +67,8 @@ export function decorateCollection(
BaseClass: typeof PureCollection,
): ICollectionConstructor<PureCollection & IJsonapiCollection> {
class JsonapiCollection extends BaseClass {
public static [DATX_JSONAPI_CLASS] = true;

public static types =
BaseClass.types && BaseClass.types.length
? BaseClass.types.concat(GenericModel)
Expand Down
3 changes: 3 additions & 0 deletions packages/datx-jsonapi/src/decorateModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PureCollection, PureModel } from '@datx/core';
import { IRawModel, META_FIELD, setMeta } from '@datx/utils';

import {
DATX_JSONAPI_CLASS,
MODEL_LINKS_FIELD,
MODEL_META_FIELD,
MODEL_PERSISTED_FIELD,
Expand All @@ -24,6 +25,8 @@ const HYDRATIZATION_KEYS = [

export function decorateModel(BaseClass: typeof PureModel): typeof PureModel {
class JsonapiModel extends BaseClass {
public static [DATX_JSONAPI_CLASS] = true;

/**
* Should the autogenerated ID be sent to the server when creating a record
*
Expand Down
3 changes: 3 additions & 0 deletions packages/datx-jsonapi/src/decorateView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
PureModel,
View,
} from '@datx/core';
import { DATX_JSONAPI_CLASS } from './consts';
import { getAllResponses } from './helpers/utils';
import { IGetAllResponse } from './interfaces/IGetAllResponse';

Expand All @@ -20,6 +21,8 @@ export function decorateView<U>(
BaseClass: typeof View,
): IViewConstructor<IJsonapiModel, U & IJsonapiView> {
class JsonapiView<M extends IJsonapiModel = IJsonapiModel> extends BaseClass {
public static [DATX_JSONAPI_CLASS] = true;

protected __collection: IJsonapiCollection & PureCollection;

constructor(
Expand Down
7 changes: 6 additions & 1 deletion packages/datx-jsonapi/src/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IFieldDefinition, IReferenceDefinition, PureModel } from '@datx/core';
import { Collection, IFieldDefinition, IReferenceDefinition, PureModel, View } from '@datx/core';
import { getMeta } from '@datx/utils';
import { DATX_JSONAPI_CLASS } from '../consts';
import { IGetAllResponse } from '../interfaces/IGetAllResponse';
import { IJsonapiModel } from '../interfaces/IJsonapiModel';
import { Response } from '../Response';
Expand Down Expand Up @@ -76,3 +77,7 @@ export async function getAllResponses<M extends IJsonapiModel = IJsonapiModel>(
lastResponse,
};
}

export function isJsonApiClass(type: typeof PureModel | typeof Collection | typeof View): boolean {
return DATX_JSONAPI_CLASS in type;
}
14 changes: 10 additions & 4 deletions packages/datx-jsonapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ export {
saveModel,
} from './helpers/model';

export {
prepareQuery,
buildUrl
} from './helpers/url';
export { prepareQuery, buildUrl } from './helpers/url';
export { isJsonApiClass } from './helpers/utils';

export { BaseJsonapiRequest } from './BaseRequest';

Expand Down Expand Up @@ -63,3 +61,11 @@ export {
clearAllCache,
clearCacheByType,
} from '@datx/network';

export {
MODEL_LINKS_FIELD,
MODEL_META_FIELD,
MODEL_PERSISTED_FIELD,
MODEL_REF_LINKS_FIELD,
MODEL_REF_META_FIELD,
} from './consts';
20 changes: 18 additions & 2 deletions packages/datx-jsonapi/test/general.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Collection, getModelId, getModelType, Model } from '@datx/core';
import { Collection, getModelId, getModelType, Model, PureModel, View } from '@datx/core';
import { mobx } from '@datx/utils';

import { getModelRefLinks, jsonapi, modelToJsonApi } from '../src';
import { getModelRefLinks, isJsonApiClass, jsonapi, modelToJsonApi } from '../src';
import { Event, Image, Photo, TestStore, User } from './utils/setup';

describe('General', () => {
Expand Down Expand Up @@ -715,4 +715,20 @@ describe('General', () => {
expect(data.relationships.image.data).toBeNull();
}
});

it('should detect jsonapi classes', () => {
class PlainModel extends Model {}
class JsonapiModel extends jsonapi(Model) {}
class PlainCollection extends Collection {}
class JsonapiCollection extends jsonapi(Collection) {}
class PlainView extends View {}
class JsonapiView extends jsonapi(View<PureModel>) {}

expect(isJsonApiClass(PlainModel)).toBe(false);
expect(isJsonApiClass(JsonapiModel)).toBe(true);
expect(isJsonApiClass(PlainCollection)).toBe(false);
expect(isJsonApiClass(JsonapiCollection)).toBe(true);
expect(isJsonApiClass(PlainView as typeof View)).toBe(false);
expect(isJsonApiClass(JsonapiView as typeof View)).toBe(true);
});
});
13 changes: 0 additions & 13 deletions packages/datx-jsonapi/test/issues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { getModelMeta, getModelRefMeta, jsonapi, modelToJsonApi, config } from '
import { setupNetwork, setRequest, confirmNetwork } from './utils/api';
import { Event, LineItem, TestStore } from './utils/setup';
import { clearAllCache } from '../src/cache';
import { flattenModel } from '../src/helpers/model';

describe('Issues', () => {
beforeEach(() => {
Expand Down Expand Up @@ -444,18 +443,6 @@ describe('Issues', () => {

const newOrder1 = new Order({ created_at: new Date(), retrieveAt: new Date() });
const newOrder2 = store.add({ created_at: new Date(), retrieveAt: new Date() }, Order);
console.log(
flattenModel(
{
[Order.type]: Order,
},
{
id: '123',
type: 'orders',
attributes: { created_at: new Date(), retrieve_at: new Date() },
},
),
);
const newOrder3 = store.sync({
data: {
id: '123',
Expand Down
8 changes: 8 additions & 0 deletions website/versioned_docs/version-2.0.0/jsonapi/jsonapi-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,11 @@ clearCacheByType(type: IType);
```

Clear the network cache for the given model type.

### isJsonApiClass

```typescript
function isJsonApiClass(type: typeof PureModel | typeof Collection | typeof View): boolean;
```

Check if the given type is a JSON API class (was decorated with `jsonapi()`).

0 comments on commit 159158f

Please sign in to comment.