Skip to content

Commit

Permalink
chore: make json-api cache strict (#9017)
Browse files Browse the repository at this point in the history
* chore: make json-api cache strict

* fix test
  • Loading branch information
runspired authored Oct 19, 2023
1 parent 2920d12 commit 734ea94
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 102 deletions.
4 changes: 0 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,6 @@ module.exports = {
'tests/fastboot/app/config/environment.d.ts',
'tests/fastboot/app/app.ts',
'tests/fastboot/app/adapters/application.ts',
'packages/json-api/src/-private/cache.ts',
'packages/model/src/index.ts',
'packages/model/src/-private/util.ts',
'packages/model/src/-private/relationship-meta.ts',
'packages/model/src/-private/legacy-relationships-support.ts',
Expand All @@ -240,14 +238,12 @@ module.exports = {
'packages/adapter/types/require/index.d.ts',
'packages/adapter/src/rest.ts',
'packages/adapter/src/json-api.ts',
'packages/adapter/src/index.ts',
'packages/adapter/src/-private/utils/serialize-query-params.ts',
'packages/adapter/src/-private/utils/fetch.ts',
'packages/adapter/src/-private/utils/determine-body-promise.ts',
'packages/adapter/src/-private/utils/continue-on-reject.ts',
'packages/adapter/src/-private/fastboot-interface.ts',
'packages/adapter/src/-private/build-url-mixin.ts',
'packages/-ember-data/addon/store.ts',
'tests/main/tests/integration/request-state-service-test.ts',
'tests/main/tests/integration/record-data/store-wrapper-test.ts',
'tests/main/tests/integration/record-data/record-data-test.ts',
Expand Down
6 changes: 3 additions & 3 deletions ember-data-types/q/minimum-serializer-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { ModelSchema } from './ds-model';
import type { JsonApiDocument, SingleResourceDocument } from './ember-data-json-api';
import type { AdapterPayload } from './minimum-adapter-interface';

export type OptionsHash = Record<string, unknown>;
export type SerializerOptions = { includeId?: boolean };
export type RequestType =
| 'findRecord'
| 'queryRecord'
Expand Down Expand Up @@ -107,7 +107,7 @@ export interface MinimumSerializerInterface {
* @param {Snapshot} snapshot A Snapshot for the record to serialize
* @param {object} [options]
*/
serialize(snapshot: Snapshot, options?: OptionsHash): JSONObject;
serialize(snapshot: Snapshot, options?: SerializerOptions): JSONObject;

/**
* This method is intended to normalize data into a [JSON:API Document](https://jsonapi.org/format/#document-structure)
Expand Down Expand Up @@ -205,7 +205,7 @@ export interface MinimumSerializerInterface {
* @param [options]
* @returns {void}
*/
serializeIntoHash?(hash: object, schema: ModelSchema, snapshot: Snapshot, options?: OptionsHash): void;
serializeIntoHash?(hash: object, schema: ModelSchema, snapshot: Snapshot, options?: SerializerOptions): void;

/**
* This method allows for normalization of data when `store.pushPayload` is called
Expand Down
5 changes: 3 additions & 2 deletions packages/-ember-data/addon/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { Cache } from '@ember-data/types/cache/cache';
import type { CacheCapabilitiesManager } from '@ember-data/types/q/cache-store-wrapper';
import type { ModelSchema } from '@ember-data/types/q/ds-model';
import type { StableRecordIdentifier } from '@ember-data/types/q/identifier';
import type { SerializerOptions } from '@ember-data/types/q/minimum-serializer-interface';

export default class Store extends BaseStore {
constructor(args: Record<string, unknown>) {
Expand All @@ -34,15 +35,15 @@ export default class Store extends BaseStore {
}

teardownRecord(record: Model): void {
teardownRecord.call(this, record as Model);
teardownRecord.call(this, record);
}

modelFor(type: string): ModelSchema {
return modelFor.call(this, type) || super.modelFor(type);
}

// TODO @runspired @deprecate records should implement their own serialization if desired
serializeRecord(record: unknown, options?: Record<string, unknown>): unknown {
serializeRecord(record: unknown, options?: SerializerOptions): unknown {
// TODO we used to check if the record was destroyed here
if (!this._fetchManager) {
this._fetchManager = new FetchManager(this);
Expand Down
9 changes: 2 additions & 7 deletions packages/adapter/src/-private/utils/serialize-into-hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ import type { Snapshot } from 'ember-data/-private';

import type Store from '@ember-data/store';
import type { ModelSchema } from '@ember-data/types/q/ds-model';
import type { MinimumSerializerInterface } from '@ember-data/types/q/minimum-serializer-interface';
import type { MinimumSerializerInterface, SerializerOptions } from '@ember-data/types/q/minimum-serializer-interface';

type SerializerWithSerializeIntoHash = MinimumSerializerInterface & {
serializeIntoHash?(
hash: object,
modelClass: ModelSchema,
snapshot: Snapshot,
options?: { includeId?: boolean }
): void;
serializeIntoHash?(hash: object, modelClass: ModelSchema, snapshot: Snapshot, options?: SerializerOptions): void;
};

export default function serializeIntoHash(
Expand Down
11 changes: 9 additions & 2 deletions packages/adapter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ By default when using with Ember you only need to implement this hook if you wan
@main @ember-data/adapter
*/

import { assert } from '@ember/debug';
import EmberObject from '@ember/object';
import { inject as service } from '@ember/service';

Expand All @@ -195,6 +196,7 @@ import type { Snapshot, SnapshotRecordArray } from '@ember-data/legacy-compat/-p
import type Store from '@ember-data/store';
import type { ModelSchema } from '@ember-data/types/q/ds-model';
import type { AdapterPayload, MinimumAdapterInterface } from '@ember-data/types/q/minimum-adapter-interface';
import { SerializerOptions } from '@ember-data/types/q/minimum-serializer-interface';

/**
An adapter is an object that receives requests from a store and
Expand Down Expand Up @@ -486,8 +488,13 @@ export default class Adapter extends EmberObject implements MinimumAdapterInterf
@return {Object} serialized snapshot
@public
*/
serialize(snapshot, options): Record<string, unknown> {
return snapshot.serialize(options);
serialize(snapshot: Snapshot, options: SerializerOptions): Record<string, unknown> {
const serialized = snapshot.serialize(options);
assert(
`Your adapter's serialize method must return an object, but it returned ${typeof serialized}`,
serialized && typeof serialized === 'object'
);
return serialized as Record<string, unknown>;
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/graph/src/-private.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { graphFor, peekGraph } from './-private/index';
export { isBelongsTo } from './-private/-utils';

/**
* <p align="center">
Expand Down
Loading

0 comments on commit 734ea94

Please sign in to comment.