Skip to content

Commit 9bbe9fd

Browse files
committed
fix(json-api-nestjs-sdk): Fix check for relation and add type for meta data
1 parent 7953371 commit 9bbe9fd

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

libs/json-api/json-api-nestjs-sdk/src/lib/service/json-api-utils.service.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,19 @@ export class JsonApiUtilsService {
144144
httpParams = httpParams.set(
145145
`filter[${key}][${operand}]`,
146146
Array.isArray(filter)
147-
? filter.join(',')
147+
? filter
148+
.map((f) =>
149+
this.jsonApiSdkConfig.dateFields.includes(key) &&
150+
f instanceof Date
151+
? f.toJSON()
152+
: f.toString()
153+
)
154+
.join(',')
148155
: filter === null
149156
? 'null'
157+
: this.jsonApiSdkConfig.dateFields.includes(key) &&
158+
filter instanceof Date
159+
? filter.toJSON()
150160
: filter.toString()
151161
);
152162
}
@@ -177,6 +187,14 @@ export class JsonApiUtilsService {
177187
body: ResourceObject<E, 'array'>,
178188
includeEntity?: QueryParams<E>['include']
179189
): E[];
190+
convertResponseData<E, M>(
191+
body: ResourceObject<E, 'object', M>,
192+
includeEntity?: QueryParams<E>['include']
193+
): E;
194+
convertResponseData<E, M>(
195+
body: ResourceObject<E, 'array', M>,
196+
includeEntity?: QueryParams<E>['include']
197+
): E[];
180198
convertResponseData<E>(
181199
body: ResourceObject<E, 'array'> | ResourceObject<E>,
182200
includeEntity?: QueryParams<E>['include']
@@ -241,7 +259,7 @@ export class JsonApiUtilsService {
241259
return result;
242260
}
243261

244-
private createEntityInstance<E>(name: string): E {
262+
createEntityInstance<E>(name: string): E {
245263
const entityName = kebabToCamel(name);
246264
return Function('return new class ' + entityName + '{}')();
247265
}
@@ -297,9 +315,8 @@ export class JsonApiUtilsService {
297315

298316
const relationships = ObjectTyped.entries(entity)
299317
.filter(([key, val]) => {
300-
if (key === 'id') return false;
318+
if (key === ID_KEY) return false;
301319
const item = Array.isArray(val) ? val[0] : val;
302-
// console.log(key, val, isRelation(item));
303320
return isRelation(item);
304321
})
305322
.reduce((acum, [key, val]) => {

libs/json-api/json-api-nestjs-sdk/src/lib/utils/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ export function getTypeForReq(str: string): string {
3333
}
3434

3535
export function isRelation(val: any): boolean {
36-
return !(
36+
const result = !(
3737
val === null ||
3838
!val ||
3939
['String', 'Boolean', 'Number', 'Date'].includes(val.constructor.name)
4040
);
41+
42+
if (!result) return result;
43+
return ID_KEY in val;
4144
}

libs/json-api/json-shared-type/src/types/response-body.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,16 @@ export type ResourceData<T> = MainData & {
5050
links: Omit<Links, 'related'>;
5151
};
5252

53-
export type ResourceObject<T, R extends 'object' | 'array' = 'object'> = {
54-
meta: R extends 'array' ? PageProps & DebugMetaProps : DebugMetaProps;
53+
export type MetaProps<T, R = null> = R extends null ? T : T & R;
54+
55+
export type ResourceObject<
56+
T,
57+
R extends 'object' | 'array' = 'object',
58+
M = null
59+
> = {
60+
meta: R extends 'array'
61+
? MetaProps<PageProps & DebugMetaProps, M>
62+
: MetaProps<DebugMetaProps, M>;
5563
data: R extends 'array' ? ResourceData<T>[] : ResourceData<T>;
5664
included?: Include<T>[];
5765
};

0 commit comments

Comments
 (0)