|
| 1 | +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ |
| 2 | +import { schema, AbstractInstanceType, DELETED } from '@rest-hooks/normalizr'; |
| 3 | + |
| 4 | +export default class Delete<E extends schema.EntityInterface & { fromJS: any }> |
| 5 | + implements schema.SchemaClass |
| 6 | +{ |
| 7 | + private declare _entity: E; |
| 8 | + |
| 9 | + constructor(entity: E) { |
| 10 | + if (process.env.NODE_ENV !== 'production' && !entity) { |
| 11 | + throw new Error('Expected option "entity" not found on DeleteSchema.'); |
| 12 | + } |
| 13 | + this._entity = entity; |
| 14 | + } |
| 15 | + |
| 16 | + get key() { |
| 17 | + return this._entity.key; |
| 18 | + } |
| 19 | + |
| 20 | + normalize( |
| 21 | + input: any, |
| 22 | + parent: any, |
| 23 | + key: string | undefined, |
| 24 | + visit: (...args: any) => any, |
| 25 | + addEntity: (...args: any) => any, |
| 26 | + visitedEntities: Record<string, any>, |
| 27 | + ): string | undefined { |
| 28 | + // pass over already processed entities |
| 29 | + if (typeof input === 'string') return input; |
| 30 | + // TODO: what's store needs to be a differing type from fromJS |
| 31 | + const processedEntity = this._entity.fromJS(input, parent, key); |
| 32 | + const id = processedEntity.pk(parent, key); |
| 33 | + if ( |
| 34 | + process.env.NODE_ENV !== 'production' && |
| 35 | + (id === undefined || id === '') |
| 36 | + ) { |
| 37 | + const error = new Error( |
| 38 | + `Missing usable primary key when normalizing response. |
| 39 | +
|
| 40 | + This is likely due to a malformed response. |
| 41 | + Try inspecting the network response or fetch() return value. |
| 42 | + Or use debugging tools: https://resthooks.io/docs/guides/debugging |
| 43 | + Learn more about schemas: https://resthooks.io/docs/api/schema |
| 44 | +
|
| 45 | + Delete(Entity): Delete(${(this._entity as any).name ?? this._entity}) |
| 46 | + Value: ${input && JSON.stringify(input, null, 2)} |
| 47 | + `, |
| 48 | + ); |
| 49 | + (error as any).status = 400; |
| 50 | + throw error; |
| 51 | + } |
| 52 | + addEntity(this, DELETED, id); |
| 53 | + return id; |
| 54 | + } |
| 55 | + |
| 56 | + infer(args: any, indexes: any, recurse: any): any { |
| 57 | + return undefined; |
| 58 | + } |
| 59 | + |
| 60 | + denormalize( |
| 61 | + id: string, |
| 62 | + unvisit: schema.UnvisitFunction, |
| 63 | + ): [AbstractInstanceType<E>, boolean, boolean] { |
| 64 | + return unvisit(id, this._entity) as any; |
| 65 | + } |
| 66 | + |
| 67 | + /* istanbul ignore next */ |
| 68 | + _denormalizeNullable(): [ |
| 69 | + AbstractInstanceType<E> | undefined, |
| 70 | + boolean, |
| 71 | + false, |
| 72 | + ] { |
| 73 | + return [] as any; |
| 74 | + } |
| 75 | + |
| 76 | + /* istanbul ignore next */ |
| 77 | + _normalizeNullable(): string | undefined { |
| 78 | + return [] as any; |
| 79 | + } |
| 80 | + |
| 81 | + /* istanbul ignore next */ |
| 82 | + merge(existing: any, incoming: any) { |
| 83 | + return incoming; |
| 84 | + } |
| 85 | +} |
0 commit comments