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

chore: cleanup ember-data/-private types #9021

Merged
merged 7 commits into from
Oct 19, 2023
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
12 changes: 0 additions & 12 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,22 +222,10 @@ module.exports = {
'@typescript-eslint/unbound-method': 'off',
},
files: [
'tests/fastboot/types/global.d.ts',
'tests/fastboot/app/serializers/application.ts',
'tests/fastboot/app/router.ts',
'tests/fastboot/app/resolver.ts',
'tests/fastboot/app/config/environment.d.ts',
'tests/fastboot/app/app.ts',
'tests/fastboot/app/adapters/application.ts',
'packages/adapter/src/rest.ts',
'packages/adapter/src/json-api.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',
'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',
'tests/main/tests/integration/model-errors-test.ts',
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ jobs:
env:
EMBER_DATA_FEATURE_OVERRIDE: DISABLE_ALL
run: pnpm test
- name: Production build
run: pnpm test:production
- if: |
github.event_name == 'pull_request' && (
github.base_ref == 'main' || github.base_ref == 'beta'
Expand Down
31 changes: 20 additions & 11 deletions ember-data-types/q/ds-model.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import type Model from '@ember-data/model';
import { AttributeSchema, RelationshipSchema } from './record-data-schemas';

export type ModelSchema = Pick<
typeof Model,
| 'modelName'
| 'fields'
| 'attributes'
| 'relationshipsByName'
| 'eachAttribute'
| 'eachRelationship'
| 'eachTransformedAttribute'
>;
type GenericRecord = Record<string, unknown>;
export interface ModelSchema<T extends object = GenericRecord> {
modelName: string;
fields: Map<keyof T & string, 'attribute' | 'belongsTo' | 'hasMany'>;
attributes: Map<keyof T & string, AttributeSchema>;
relationshipsByName: Map<keyof T & string, RelationshipSchema>;
eachAttribute<K extends keyof T & string>(
callback: (this: ModelSchema<T>, key: K, attribute: AttributeSchema) => void,
binding?: T
): void;
eachRelationship<K extends keyof T & string>(
callback: (this: ModelSchema<T>, key: K, relationship: RelationshipSchema) => void,
binding?: T
): void;
eachTransformedAttribute<K extends keyof T & string>(
callback: (this: ModelSchema<T>, key: K, type: string | null) => void,
binding?: T
): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import Ember from 'ember';

import VERSION from 'ember-data/version';

const DS = Namespace.create({
export interface DS extends Namespace {
VERSION: string;
name: string;
}

export const DS = Namespace.create({
// @ts-expect-error ember-source types are wrong
VERSION: VERSION,
name: 'DS',
});
Expand Down
2 changes: 1 addition & 1 deletion packages/-ember-data/addon/-private/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ deprecate('Importing from `ember-data/-private` is deprecated without replacemen

export { default as Store } from '../store';

export { default as DS } from './core';
export { DS } from './core';
export { Errors } from '@ember-data/model/-private';
export { Snapshot } from '@ember-data/legacy-compat/-private';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,61 +215,90 @@ deprecate(
}
);

interface DSLibrary extends DS {
Store: typeof Store;
PromiseArray: typeof PromiseArray;
PromiseObject: typeof PromiseObject;
PromiseManyArray: typeof PromiseManyArray;
Model: typeof Model;
attr: attr;
Errors: typeof Errors;
Snapshot: typeof Snapshot;
Adapter: typeof Adapter;
AdapterError: typeof AdapterError;
InvalidError: typeof InvalidError;
TimeoutError: typeof TimeoutError;
AbortError: typeof AbortError;
UnauthorizedError: typeof UnauthorizedError;
ForbiddenError: typeof ForbiddenError;
NotFoundError: typeof NotFoundError;
ConflictError: typeof ConflictError;
ServerError: typeof ServerError;
Serializer: typeof Serializer;
DebugAdapter?: typeof import('@ember-data/debug').default;
ManyArray: typeof ManyArray;
RecordArrayManager: typeof RecordArrayManager;
RESTAdapter: typeof RESTAdapter;
BuildURLMixin: typeof BuildURLMixin;
RESTSerializer: typeof RESTSerializer;
JSONSerializer: typeof JSONSerializer;
JSONAPIAdapter: typeof JSONAPIAdapter;
JSONAPISerializer: typeof JSONAPISerializer;
Transform: typeof Transform;
DateTransform: typeof DateTransform;
StringTransform: typeof StringTransform;
NumberTransform: typeof NumberTransform;
BooleanTransform: typeof BooleanTransform;
EmbeddedRecordsMixin: typeof EmbeddedRecordsMixin;
belongsTo: typeof belongsTo;
hasMany: typeof hasMany;
_setupContainer: typeof setupContainer;
}

function upgradeDS(obj: unknown): asserts obj is DSLibrary {}

upgradeDS(DS);

DS.Store = Store;
DS.PromiseArray = PromiseArray;
DS.PromiseObject = PromiseObject;

DS.PromiseManyArray = PromiseManyArray;

DS.Model = Model;
DS.attr = attr;
DS.Errors = Errors;

DS.Snapshot = Snapshot;

DS.Adapter = Adapter;

DS.AdapterError = AdapterError;
DS.InvalidError = InvalidError;
DS.TimeoutError = TimeoutError;
DS.AbortError = AbortError;

DS.UnauthorizedError = UnauthorizedError;
DS.ForbiddenError = ForbiddenError;
DS.NotFoundError = NotFoundError;
DS.ConflictError = ConflictError;
DS.ServerError = ServerError;

DS.Serializer = Serializer;

if (macroCondition(dependencySatisfies('@ember-data/debug', '*'))) {
DS.DebugAdapter = importSync('@ember-data/debug').default;
DS.DebugAdapter = (importSync('@ember-data/debug') as typeof import('@ember-data/debug')).default;
}

DS.ManyArray = ManyArray;

DS.RecordArrayManager = RecordArrayManager;

DS.RESTAdapter = RESTAdapter;
DS.BuildURLMixin = BuildURLMixin;

DS.RESTSerializer = RESTSerializer;
DS.JSONSerializer = JSONSerializer;

DS.JSONAPIAdapter = JSONAPIAdapter;
DS.JSONAPISerializer = JSONAPISerializer;

DS.Transform = Transform;
DS.DateTransform = DateTransform;
DS.StringTransform = StringTransform;
DS.NumberTransform = NumberTransform;
DS.BooleanTransform = BooleanTransform;

DS.EmbeddedRecordsMixin = EmbeddedRecordsMixin;

DS.belongsTo = belongsTo;
DS.hasMany = hasMany;

DS._setupContainer = setupContainer;

export default DS;
3 changes: 3 additions & 0 deletions packages/-ember-data/addon/version.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const VERSION: string;

export default VERSION;
2 changes: 1 addition & 1 deletion packages/adapter/src/-private/build-url-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ function buildURL(
@return {String} url
*/
function _buildURL(this: MixtBuildURLMixin, modelName: string | null | undefined, id?: string | null): string {
let path;
let path: string;
let url: string[] = [];
let { host } = this;
let prefix = this.urlPrefix();
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter/src/-private/utils/continue-on-reject.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default function continueOnReject<T>(promise: Promise<T>): Promise<T> {
return Promise.resolve(promise).catch((e) => e);
return Promise.resolve(promise).catch((e) => e as T);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function _determineContent(response: Response, requestData: JQueryAjaxSettings,
}

try {
ret = JSON.parse(payload as string);
ret = JSON.parse(payload as string) as Payload;
} catch (e) {
if (!(e instanceof SyntaxError)) {
return e as Error;
Expand Down
21 changes: 11 additions & 10 deletions packages/model/src/-private/model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ class Model extends EmberObject {
adapterError?: Error;
toString(): string;
save(): Promise<this>;
hasMany(key: string): HasManyReference;
belongsTo(key: string): BelongsToReference
eachRelationship<T>(callback: (this: T, key: string, meta: RelationshipSchema) => void, binding?: T): void;
eachAttribute<T>(callback: (this: T, key: string, meta: AttributeSchema) => void, binding?: T): void;
hasMany(key: keyof this & string): HasManyReference;
belongsTo(key: keyof this & string): BelongsToReference
eachRelationship<T extends Model, K extends keyof T & string>(callback: (this: T, key: K, meta: RelationshipSchema) => void, binding?: T): void;
eachAttribute<T extends Model, K extends keyof T & string>(callback: (this: T, key: K, meta: AttributeSchema) => void, binding?: T): void;
invalidErrorsChanged(errors: JsonApiError[]): void;
rollbackAttributes(): void;
changedAttributes(): Record<string, [unknown, unknown]>;
id: string;
[key: string]: unknown;
isSaving: boolean;
isNew: boolean;
Expand All @@ -48,12 +49,12 @@ class Model extends EmberObject {
serialize(): Record<string, unknown>;

static modelName: string;
static fields: Map<string, 'attribute' | 'belongsTo' | 'hasMany'>;
static attributes: Map<string, AttributeSchema>;
static relationshipsByName: Map<string, RelationshipSchema>;
static eachAttribute<T>(callback: (this: T, key: string, attribute: AttributeSchema) => void, binding?: T): void;
static eachRelationship<T>(callback: (this: T, key: string, relationship: RelationshipSchema) => void, binding?: T): void;
static eachTransformedAttribute<T>(callback: (this: T, key: string, type: string | null) => void, binding?: T): void;
static fields: Map<keyof this & string, 'attribute' | 'belongsTo' | 'hasMany'>;
static attributes: Map<keyof this & string, AttributeSchema>;
static relationshipsByName: Map<keyof this & string, RelationshipSchema>;
static eachAttribute<K extends keyof this & string>(callback: (this: ModelSchema<this>, key: K, attribute: AttributeSchema) => void, binding?: T): void;
static eachRelationship<K extends keyof this & string>(callback: (this: ModelSchema<this>, key: K, relationship: RelationshipSchema) => void, binding?: T): void;
static eachTransformedAttribute<K extends keyof this & string>(callback: (this: ModelSchema<this>, key: K, type: string | null) => void, binding?: T): void;

static toString(): string;
static isModel: true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,24 @@
@public
*/
export default class BooleanTransform {
deserialize(serialized, options) {
if ((serialized === null || serialized === undefined) && options.allowNull === true) {
deserialize(serialized: boolean | null | number | string, options?: { allowNull?: boolean }): boolean | null {
if ((serialized === null || serialized === undefined) && options?.allowNull === true) {
return null;
}

let type = typeof serialized;
if (type === 'boolean') {
if (typeof serialized === 'boolean') {
return serialized;
} else if (type === 'string') {
} else if (typeof serialized === 'string') {
return /^(true|t|1)$/i.test(serialized);
} else if (type === 'number') {
} else if (typeof serialized === 'number') {
return serialized === 1;
} else {
return false;
}
}

serialize(deserialized, options) {
if ((deserialized === null || deserialized === undefined) && options.allowNull === true) {
serialize(deserialized: boolean | null, options?: { allowNull?: boolean }): boolean | null {
if ((deserialized === null || deserialized === undefined) && options?.allowNull === true) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,16 @@
*/

export default class DateTransform {
deserialize(serialized) {
let type = typeof serialized;

if (type === 'string') {
deserialize(serialized: string | number | null, _options?: Record<string, unknown>) {
if (typeof serialized === 'string') {
let offset = serialized.indexOf('+');

if (offset !== -1 && serialized.length - 5 === offset) {
offset += 3;
return new Date(serialized.slice(0, offset) + ':' + serialized.slice(offset));
}
return new Date(serialized);
} else if (type === 'number') {
} else if (typeof serialized === 'number') {
return new Date(serialized);
} else if (serialized === null || serialized === undefined) {
// if the value is null return null
Expand All @@ -46,7 +44,8 @@ export default class DateTransform {
}
}

serialize(date) {
serialize(date: Date, _options?: Record<string, unknown>): string | null {
// @ts-expect-error isNaN accepts date as it is coercible
if (date instanceof Date && !isNaN(date)) {
return date.toISOString();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,21 @@ function isNumber(value) {
@public
*/
export default class NumberTransform {
deserialize(serialized) {
let transformed;

deserialize(serialized: string | number | null | undefined, _options?: Record<string, unknown>): number | null {
if (serialized === '' || serialized === null || serialized === undefined) {
return null;
} else {
transformed = Number(serialized);
const transformed = Number(serialized);

return isNumber(transformed) ? transformed : null;
}
}

serialize(deserialized) {
let transformed;

serialize(deserialized: string | number | null | undefined, _options?: Record<string, unknown>): number | null {
if (deserialized === '' || deserialized === null || deserialized === undefined) {
return null;
} else {
transformed = Number(deserialized);
const transformed = Number(deserialized);

return isNumber(transformed) ? transformed : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
@public
*/
export default class StringTransform {
deserialize(serialized) {
deserialize(serialized: unknown, _options?: Record<string, unknown>): string | null {
return !serialized && serialized !== '' ? null : String(serialized);
}
serialize(deserialized) {
serialize(deserialized: unknown, _options?: Record<string, unknown>): string | null {
return !deserialized && deserialized !== '' ? null : String(deserialized);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/**
@module @ember-data/serializer
*/
import EmberObject from '@ember/object';

import { AttributeSchema } from '@ember-data/types/q/record-data-schemas';

/**
The `Transform` class is used to serialize and deserialize model
Expand Down Expand Up @@ -115,4 +118,10 @@
@param options hash of options passed to `attr`
@return The deserialized value
*/
export { default } from '@ember/object';
interface Transform {
serialize(value: unknown, options: AttributeSchema['options']): unknown;
deserialize(value: unknown, options: AttributeSchema['options']): unknown;
}
const Transform = EmberObject;

export default Transform;
Loading
Loading