Skip to content

Commit

Permalink
fix(json-api-nestjs-sdk): Fix check for relation and add type for met…
Browse files Browse the repository at this point in the history
…a data
  • Loading branch information
klerick committed Dec 5, 2024
1 parent 7953371 commit 9bbe9fd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,19 @@ export class JsonApiUtilsService {
httpParams = httpParams.set(
`filter[${key}][${operand}]`,
Array.isArray(filter)
? filter.join(',')
? filter
.map((f) =>
this.jsonApiSdkConfig.dateFields.includes(key) &&
f instanceof Date
? f.toJSON()
: f.toString()
)
.join(',')
: filter === null
? 'null'
: this.jsonApiSdkConfig.dateFields.includes(key) &&
filter instanceof Date
? filter.toJSON()
: filter.toString()
);
}
Expand Down Expand Up @@ -177,6 +187,14 @@ export class JsonApiUtilsService {
body: ResourceObject<E, 'array'>,
includeEntity?: QueryParams<E>['include']
): E[];
convertResponseData<E, M>(
body: ResourceObject<E, 'object', M>,
includeEntity?: QueryParams<E>['include']
): E;
convertResponseData<E, M>(
body: ResourceObject<E, 'array', M>,
includeEntity?: QueryParams<E>['include']
): E[];
convertResponseData<E>(
body: ResourceObject<E, 'array'> | ResourceObject<E>,
includeEntity?: QueryParams<E>['include']
Expand Down Expand Up @@ -241,7 +259,7 @@ export class JsonApiUtilsService {
return result;
}

private createEntityInstance<E>(name: string): E {
createEntityInstance<E>(name: string): E {
const entityName = kebabToCamel(name);
return Function('return new class ' + entityName + '{}')();
}
Expand Down Expand Up @@ -297,9 +315,8 @@ export class JsonApiUtilsService {

const relationships = ObjectTyped.entries(entity)
.filter(([key, val]) => {
if (key === 'id') return false;
if (key === ID_KEY) return false;
const item = Array.isArray(val) ? val[0] : val;
// console.log(key, val, isRelation(item));
return isRelation(item);
})
.reduce((acum, [key, val]) => {
Expand Down
5 changes: 4 additions & 1 deletion libs/json-api/json-api-nestjs-sdk/src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ export function getTypeForReq(str: string): string {
}

export function isRelation(val: any): boolean {
return !(
const result = !(
val === null ||
!val ||
['String', 'Boolean', 'Number', 'Date'].includes(val.constructor.name)
);

if (!result) return result;
return ID_KEY in val;
}
12 changes: 10 additions & 2 deletions libs/json-api/json-shared-type/src/types/response-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,16 @@ export type ResourceData<T> = MainData & {
links: Omit<Links, 'related'>;
};

export type ResourceObject<T, R extends 'object' | 'array' = 'object'> = {
meta: R extends 'array' ? PageProps & DebugMetaProps : DebugMetaProps;
export type MetaProps<T, R = null> = R extends null ? T : T & R;

export type ResourceObject<
T,
R extends 'object' | 'array' = 'object',
M = null
> = {
meta: R extends 'array'
? MetaProps<PageProps & DebugMetaProps, M>
: MetaProps<DebugMetaProps, M>;
data: R extends 'array' ? ResourceData<T>[] : ResourceData<T>;
included?: Include<T>[];
};
Expand Down

0 comments on commit 9bbe9fd

Please sign in to comment.