Skip to content

Commit

Permalink
enhance: Update denormalize() args order
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Jun 25, 2024
1 parent af06dd3 commit e7b145e
Show file tree
Hide file tree
Showing 46 changed files with 324 additions and 328 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@ typings/

# build info
**/tsconfig*.tsbuildinfo

/codemods
4 changes: 2 additions & 2 deletions packages/core/src/controller/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default class Controller<

if (endpoint.schema) {
return action.meta.promise.then(input =>
denormalize(input, endpoint.schema, {}, args),
denormalize(endpoint.schema, input, {}, args),
) as any;
}
return action.meta.promise as any;
Expand Down Expand Up @@ -464,8 +464,8 @@ export default class Controller<
// second argument is false if any entities are missing
// eslint-disable-next-line prefer-const
const { data, paths } = this.memo.denormalize(
input,
schema,
input,
state.entities,
args,
) as { data: any; paths: EntityPath[] };
Expand Down
2 changes: 1 addition & 1 deletion packages/endpoint/src-4.0-types/schemas/Entity.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default abstract class Entity extends Entity_base {
this: T,
input: any,
args: readonly any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
) => AbstractInstanceType<T>;
}
export {};
2 changes: 1 addition & 1 deletion packages/endpoint/src-4.0-types/schemas/EntitySchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export interface IEntityClass<TBase extends Constructor = any> {
this: T,
input: any,
args: readonly any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
): AbstractInstanceType<T>;
/** All instance defaults set */
readonly defaults: any;
Expand Down
6 changes: 3 additions & 3 deletions packages/endpoint/src/__tests__/validateRequired.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe(`validateRequired`, () => {
const schema = MyEntity;

expect(
new SimpleMemoCache().denormalize('bob', schema, {
new SimpleMemoCache().denormalize(schema, 'bob', {
MyEntity: { bob: { name: 'bob', secondthing: 'hi' } },
}),
).toMatchInlineSnapshot(`
Expand All @@ -73,7 +73,7 @@ describe(`validateRequired`, () => {
const schema = MyEntity;

expect(
new SimpleMemoCache().denormalize('bob', schema, {
new SimpleMemoCache().denormalize(schema, 'bob', {
MyEntity: {
bob: {
name: 'bob',
Expand All @@ -93,7 +93,7 @@ describe(`validateRequired`, () => {
it('should be invalid (suspend) with required fields missing', () => {
const schema = MyEntity;

const data = new SimpleMemoCache().denormalize('bob', schema, {
const data = new SimpleMemoCache().denormalize(schema, 'bob', {
MyEntity: {
bob: {
name: 'bob',
Expand Down
2 changes: 1 addition & 1 deletion packages/endpoint/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface SchemaSimple<T = any, Args extends readonly any[] = any[]> {
denormalize(
input: {},
args: readonly any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
): T;
queryKey(
args: Args,
Expand Down
10 changes: 5 additions & 5 deletions packages/endpoint/src/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class Array<S extends Schema = Schema> implements SchemaClass {
denormalize(
input: {},
args: readonly any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
): (S extends EntityMap<infer T> ? T : Denormalize<S>)[];

queryKey(
Expand Down Expand Up @@ -145,7 +145,7 @@ export class All<
denormalize(
input: {},
args: readonly any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
): (S extends EntityMap<infer T> ? T : Denormalize<S>)[];

queryKey(
Expand Down Expand Up @@ -189,7 +189,7 @@ export class Object<O extends Record<string, any> = Record<string, Schema>>
denormalize(
input: {},
args: readonly any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
): DenormalizeObject<O>;

queryKey(
Expand Down Expand Up @@ -281,7 +281,7 @@ export interface UnionInstance<
denormalize(
input: {},
args: readonly any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
): AbstractInstanceType<Choices[keyof Choices]>;

queryKey(
Expand Down Expand Up @@ -366,7 +366,7 @@ export class Values<Choices extends Schema = any> implements SchemaClass {
denormalize(
input: {},
args: readonly any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
): Record<
string,
Choices extends EntityMap<infer T> ? T : Denormalize<Choices>
Expand Down
2 changes: 1 addition & 1 deletion packages/endpoint/src/schemaTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export interface CollectionInterface<
denormalize(
input: any,
args: readonly any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
): ReturnType<S['denormalize']>;

_denormalizeNullable(): ReturnType<S['_denormalizeNullable']>;
Expand Down
2 changes: 1 addition & 1 deletion packages/endpoint/src/schemas/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default class ArraySchema extends PolymorphicSchema {
denormalize(
input: any,
args: any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
) {
return input.map ?
input
Expand Down
4 changes: 2 additions & 2 deletions packages/endpoint/src/schemas/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export default class CollectionSchema<
denormalize(
input: any,
args: readonly any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
): ReturnType<S['denormalize']> {
return this.schema.denormalize(input, args, unvisit) as any;
}
Expand Down Expand Up @@ -351,7 +351,7 @@ function denormalize(
this: CollectionSchema<any, any>,
input: any,
args: readonly any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
): any {
return Array.isArray(input) ?
(this.schema.denormalize(input, args, unvisit) as any)
Expand Down
2 changes: 1 addition & 1 deletion packages/endpoint/src/schemas/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ First three members: ${JSON.stringify(input.slice(0, 3), null, 2)}`;
this: T,
input: any,
args: readonly any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
) => AbstractInstanceType<T>;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/endpoint/src/schemas/EntitySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ export default function EntitySchema<TBase extends Constructor>(
this: T,
input: any,
args: any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
): AbstractInstanceType<T> {
if (typeof input === 'symbol') {
return input as any;
Expand All @@ -357,7 +357,7 @@ export default function EntitySchema<TBase extends Constructor>(
// note: iteration order must be stable
for (const key of Object.keys(this.schema)) {
const schema = this.schema[key];
const value = unvisit(input[key], schema);
const value = unvisit(schema, input[key]);

if (typeof value === 'symbol') {
// if default is not 'falsy', then this is required, so propagate INVALID symbol
Expand Down Expand Up @@ -668,7 +668,7 @@ export interface IEntityClass<TBase extends Constructor = any> {
this: T,
input: any,
args: readonly any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
): AbstractInstanceType<T>;
/** All instance defaults set */
readonly defaults: any;
Expand Down
4 changes: 2 additions & 2 deletions packages/endpoint/src/schemas/ImmutableUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ export function isImmutable(object: {}): object is {
export function denormalizeImmutable(
schema: any,
input: any,
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
): any {
let deleted;
const value = Object.keys(schema).reduce((object, key) => {
// Immutable maps cast keys to strings on write so we need to ensure
// we're accessing them using string keys.
const stringKey = `${key}`;

const item = unvisit(object.get(stringKey), schema[stringKey]);
const item = unvisit(schema[stringKey], object.get(stringKey));
if (typeof item === 'symbol') {
deleted = item;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/endpoint/src/schemas/Invalidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ export default class Invalidate<
denormalize(
id: string,
args: readonly any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
): AbstractInstanceType<E> {
return unvisit(id, this._entity) as any;
return unvisit(this._entity, id) as any;
}

/* istanbul ignore next */
Expand Down
6 changes: 3 additions & 3 deletions packages/endpoint/src/schemas/Object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function denormalize(
schema: any,
input: {},
args: readonly any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
): any {
if (isImmutable(input)) {
return denormalizeImmutable(schema, input, unvisit);
Expand All @@ -38,7 +38,7 @@ export function denormalize(
const object: Record<string, any> = { ...input };

for (const key of Object.keys(schema)) {
const item = unvisit(object[key], schema[key]);
const item = unvisit(schema[key], object[key]);
if (object[key] !== undefined) {
object[key] = item;
}
Expand Down Expand Up @@ -103,7 +103,7 @@ export default class ObjectSchema {
denormalize(
input: {},
args: readonly any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
): any {
return denormalize(this.schema, input, args, unvisit);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/endpoint/src/schemas/Polymorphic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Value: ${JSON.stringify(value, undefined, 2)}`,
// construct the correct Entity instance
if (typeof value === 'object' && value !== null) {
const schema = this.inferSchema(value, undefined, undefined);
if (schema) return unvisit(value, schema);
if (schema) return unvisit(schema, value);
}
/* istanbul ignore else */
if (process.env.NODE_ENV !== 'production' && value) {
Expand All @@ -107,6 +107,6 @@ Value: ${JSON.stringify(value, undefined, 2)}.`,
: isImmutable(value) ? value.get('id')
: value.id;
const schema = this.isSingleSchema ? this.schema : this.schema[schemaKey];
return unvisit(id || value, schema);
return unvisit(schema, id || value);
}
}
4 changes: 2 additions & 2 deletions packages/endpoint/src/schemas/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class Query<
}

denormalize(input: {}, args: any, unvisit: any): ReturnType<P> {
const value = unvisit(input, this.schema);
const value = unvisit(this.schema, input);
return typeof value === 'symbol' ? value : this.process(value, ...args);
}

Expand All @@ -55,7 +55,7 @@ export default class Query<
declare _denormalizeNullable: (
input: {},
args: readonly any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
) => ReturnType<P> | undefined;

declare _normalizeNullable: () => NormalizeNullable<S>;
Expand Down
2 changes: 1 addition & 1 deletion packages/endpoint/src/schemas/Union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class UnionSchema extends PolymorphicSchema {
denormalize(
input: {},
args: readonly any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
) {
return this.denormalizeValue(input, unvisit);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/endpoint/src/schemas/Values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class ValuesSchema extends PolymorphicSchema {
denormalize(
input: {},
args: readonly any[],
unvisit: (input: any, schema: any) => any,
unvisit: (schema: any, input: any) => any,
): any {
return Object.keys(input).reduce((output, key) => {
const entityOrId = (input as any)[key];
Expand Down
2 changes: 1 addition & 1 deletion packages/endpoint/src/schemas/__tests__/All.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ describe.each([
},
},
};
expect(denormalize('123', Taco, createInput(entities))).toMatchSnapshot();
expect(denormalize(Taco, '123', createInput(entities))).toMatchSnapshot();
});

test('denormalizes multiple entities', () => {
Expand Down
Loading

0 comments on commit e7b145e

Please sign in to comment.