Skip to content

Commit f72edb6

Browse files
authored
Merge pull request #36 from ctrl-hub/update-relationship-payloads
update relationship payloads
2 parents 599e99e + 72d38e7 commit f72edb6

File tree

10 files changed

+169
-90
lines changed

10 files changed

+169
-90
lines changed

dist/Client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export class Client {
219219
try {
220220
// @todo switch on cookie, "X-Session-Token" or client_credentials
221221
const fetchResponse = await fetch(url, {
222-
credentials: 'include',
222+
credentials: 'include', // @todo only required for cookie based auth,
223223
headers: headers,
224224
});
225225
let json = await fetchResponse.json();

dist/index.js

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,29 @@ class JsonApiSerializer {
12701270
relationships: {}
12711271
}
12721272
};
1273+
prototype.constructor.relationships.forEach((relationship) => {
1274+
if (relationship.type === "array") {
1275+
const value = model[relationship.name];
1276+
if (value) {
1277+
payload.data.relationships[relationship.name] = {
1278+
data: value.map((item) => ({
1279+
type: relationship.modelType,
1280+
id: item.id
1281+
}))
1282+
};
1283+
}
1284+
} else {
1285+
const value = model[relationship.name];
1286+
if (value) {
1287+
payload.data.relationships[relationship.name] = {
1288+
data: {
1289+
type: relationship.modelType,
1290+
id: value.id
1291+
}
1292+
};
1293+
}
1294+
}
1295+
});
12731296
if (mapping.attributes) {
12741297
mapping.attributes.forEach((attr) => {
12751298
const value = model[attr];
@@ -1278,19 +1301,6 @@ class JsonApiSerializer {
12781301
}
12791302
});
12801303
}
1281-
if (mapping.relationships) {
1282-
Object.entries(mapping.relationships).forEach(([key, relationshipType]) => {
1283-
const relationshipValue = model[key];
1284-
if (relationshipValue && typeof relationshipType === "string") {
1285-
payload.data.relationships[key] = {
1286-
data: {
1287-
type: relationshipType,
1288-
id: relationshipValue.id ?? relationshipValue
1289-
}
1290-
};
1291-
}
1292-
});
1293-
}
12941304
return payload;
12951305
}
12961306
return this.buildDefaultPayload(model);
@@ -1312,6 +1322,29 @@ class JsonApiSerializer {
13121322
relationships: {}
13131323
}
13141324
};
1325+
prototype.constructor.relationships.forEach((relationship) => {
1326+
if (relationship.type === "array") {
1327+
const value = model[relationship.name];
1328+
if (value) {
1329+
payload.data.relationships[relationship.name] = {
1330+
data: value.map((item) => ({
1331+
type: relationship.modelType,
1332+
id: item.id
1333+
}))
1334+
};
1335+
}
1336+
} else {
1337+
const value = model[relationship.name];
1338+
if (value) {
1339+
payload.data.relationships[relationship.name] = {
1340+
data: {
1341+
type: relationship.modelType,
1342+
id: value.id
1343+
}
1344+
};
1345+
}
1346+
}
1347+
});
13151348
if (mapping.attributes) {
13161349
mapping.attributes.forEach((attr) => {
13171350
const value = model[attr];
@@ -1320,19 +1353,6 @@ class JsonApiSerializer {
13201353
}
13211354
});
13221355
}
1323-
if (mapping.relationships) {
1324-
Object.entries(mapping.relationships).forEach(([key, relationshipType]) => {
1325-
const relationshipValue = model[key];
1326-
if (relationshipValue && typeof relationshipType === "string") {
1327-
payload.data.relationships[key] = {
1328-
data: {
1329-
type: relationshipType,
1330-
id: relationshipValue.id ?? relationshipValue
1331-
}
1332-
};
1333-
}
1334-
});
1335-
}
13361356
return payload;
13371357
}
13381358
return this.buildDefaultPayload(model);

dist/models/Operation.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export declare class Operation extends BaseModel {
99
start_date: string;
1010
end_date: string;
1111
labels: Array<Label>;
12-
uprns: Array<number>;
13-
usrns: Array<number>;
12+
uprns: Array<string>;
13+
usrns: Array<string>;
1414
completed: boolean;
1515
aborted: boolean;
1616
cancelled: boolean;

dist/services/BaseService.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ export declare class BaseService<T extends Model> extends RequestBuilder {
1313
constructor(client: Client, endpoint: string);
1414
convertToJsonApi(model: Model): {
1515
data: {
16-
id?: string | undefined;
16+
id?: string;
1717
type: string;
1818
attributes: Record<string, any>;
1919
relationships?: Record<string, {
2020
data: {
2121
type: string;
2222
id: string;
2323
};
24-
}> | undefined;
24+
}>;
2525
};
2626
};
2727
get(): Promise<InternalResponse<T[]>>;

dist/utils/JsonSerializer.js

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,30 @@ export class JsonApiSerializer {
1919
relationships: {},
2020
},
2121
};
22+
prototype.constructor.relationships.forEach((relationship) => {
23+
if (relationship.type === 'array') {
24+
const value = model[relationship.name];
25+
if (value) {
26+
payload.data.relationships[relationship.name] = {
27+
data: value.map((item) => ({
28+
type: relationship.modelType,
29+
id: item.id,
30+
})),
31+
};
32+
}
33+
}
34+
else {
35+
const value = model[relationship.name];
36+
if (value) {
37+
payload.data.relationships[relationship.name] = {
38+
data: {
39+
type: relationship.modelType,
40+
id: value.id,
41+
},
42+
};
43+
}
44+
}
45+
});
2246
if (mapping.attributes) {
2347
mapping.attributes.forEach((attr) => {
2448
const value = model[attr];
@@ -27,19 +51,6 @@ export class JsonApiSerializer {
2751
}
2852
});
2953
}
30-
if (mapping.relationships) {
31-
Object.entries(mapping.relationships).forEach(([key, relationshipType]) => {
32-
const relationshipValue = model[key];
33-
if (relationshipValue && typeof relationshipType === 'string') {
34-
payload.data.relationships[key] = {
35-
data: {
36-
type: relationshipType,
37-
id: relationshipValue.id ?? relationshipValue,
38-
},
39-
};
40-
}
41-
});
42-
}
4354
return payload;
4455
}
4556
return this.buildDefaultPayload(model);
@@ -61,6 +72,30 @@ export class JsonApiSerializer {
6172
relationships: {},
6273
},
6374
};
75+
prototype.constructor.relationships.forEach((relationship) => {
76+
if (relationship.type === 'array') {
77+
const value = model[relationship.name];
78+
if (value) {
79+
payload.data.relationships[relationship.name] = {
80+
data: value.map((item) => ({
81+
type: relationship.modelType,
82+
id: item.id,
83+
})),
84+
};
85+
}
86+
}
87+
else {
88+
const value = model[relationship.name];
89+
if (value) {
90+
payload.data.relationships[relationship.name] = {
91+
data: {
92+
type: relationship.modelType,
93+
id: value.id,
94+
},
95+
};
96+
}
97+
}
98+
});
6499
if (mapping.attributes) {
65100
mapping.attributes.forEach((attr) => {
66101
const value = model[attr];
@@ -69,19 +104,6 @@ export class JsonApiSerializer {
69104
}
70105
});
71106
}
72-
if (mapping.relationships) {
73-
Object.entries(mapping.relationships).forEach(([key, relationshipType]) => {
74-
const relationshipValue = model[key];
75-
if (relationshipValue && typeof relationshipType === 'string') {
76-
payload.data.relationships[key] = {
77-
data: {
78-
type: relationshipType,
79-
id: relationshipValue.id ?? relationshipValue,
80-
},
81-
};
82-
}
83-
});
84-
}
85107
return payload;
86108
}
87109
return this.buildDefaultPayload(model);

dist/utils/Requests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class Requests {
1212
}
1313
static buildInternalResponse(fetchResponse, json) {
1414
return {
15-
ok: fetchResponse.ok,
15+
ok: fetchResponse.ok, // @todo convert to own version
1616
statusCode: fetchResponse.status,
1717
headers: fetchResponse.headers,
1818
meta: json?.meta || null,
@@ -29,7 +29,7 @@ export class Requests {
2929
static buildInternalErrorResponse(error) {
3030
return {
3131
ok: false,
32-
statusCode: error.statusCode || 0,
32+
statusCode: error.statusCode || 0, // If there's no response, status code is 0
3333
headers: error.headers,
3434
data: null,
3535
errors: {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "git",
55
"url": "https://github.com/ctrl-hub/sdk.ts"
66
},
7-
"version": "0.1.115",
7+
"version": "0.1.116",
88
"main": "dist/index.js",
99
"types": "dist/index.d.ts",
1010
"type": "module",

src/models/Operation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export class Operation extends BaseModel {
1111
public start_date: string = '';
1212
public end_date: string = '';
1313
public labels: Array<Label> = [];
14-
public uprns: Array<number> = [];
15-
public usrns: Array<number> = [];
14+
public uprns: Array<string> = [];
15+
public usrns: Array<string> = [];
1616
public completed: boolean = false;
1717
public aborted: boolean = false;
1818
public cancelled: boolean = false;

src/services/AppointmentsService.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Client } from 'Client';
2+
import { BaseService } from './BaseService';
3+
import { Appointment } from '@models/Appointment';
4+
5+
export class AppointmentsService extends BaseService<Appointment> {
6+
constructor(client: Client) {
7+
super(client, '/v3/orgs/:orgId/appointments');
8+
}
9+
}

src/utils/JsonSerializer.ts

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,34 @@ export class JsonApiSerializer {
5252
},
5353
};
5454

55+
prototype.constructor.relationships.forEach((relationship: {
56+
name: string;
57+
type: string;
58+
modelType: string;
59+
}) => {
60+
if (relationship.type === 'array') {
61+
const value = (model as any)[relationship.name];
62+
if (value) {
63+
payload.data.relationships![relationship.name] = {
64+
data: value.map((item: any) => ({
65+
type: relationship.modelType,
66+
id: item.id,
67+
})),
68+
};
69+
}
70+
} else {
71+
const value = (model as any)[relationship.name];
72+
if (value) {
73+
payload.data.relationships![relationship.name] = {
74+
data: {
75+
type: relationship.modelType,
76+
id: value.id,
77+
},
78+
};
79+
}
80+
}
81+
});
82+
5583
if (mapping.attributes) {
5684
mapping.attributes.forEach((attr: string) => {
5785
const value = (model as any)[attr];
@@ -61,20 +89,6 @@ export class JsonApiSerializer {
6189
});
6290
}
6391

64-
if (mapping.relationships) {
65-
Object.entries(mapping.relationships).forEach(([key, relationshipType]) => {
66-
const relationshipValue = (model as any)[key];
67-
if (relationshipValue && typeof relationshipType === 'string') {
68-
payload.data.relationships![key] = {
69-
data: {
70-
type: relationshipType,
71-
id: relationshipValue.id ?? relationshipValue,
72-
},
73-
};
74-
}
75-
});
76-
}
77-
7892
return payload;
7993
}
8094

@@ -103,6 +117,34 @@ export class JsonApiSerializer {
103117
},
104118
};
105119

120+
prototype.constructor.relationships.forEach((relationship: {
121+
name: string;
122+
type: string;
123+
modelType: string;
124+
}) => {
125+
if (relationship.type === 'array') {
126+
const value = (model as any)[relationship.name];
127+
if (value) {
128+
payload.data.relationships![relationship.name] = {
129+
data: value.map((item: any) => ({
130+
type: relationship.modelType,
131+
id: item.id,
132+
})),
133+
};
134+
}
135+
} else {
136+
const value = (model as any)[relationship.name];
137+
if (value) {
138+
payload.data.relationships![relationship.name] = {
139+
data: {
140+
type: relationship.modelType,
141+
id: value.id,
142+
},
143+
};
144+
}
145+
}
146+
});
147+
106148
if (mapping.attributes) {
107149
mapping.attributes.forEach((attr: string) => {
108150
const value = (model as any)[attr];
@@ -112,20 +154,6 @@ export class JsonApiSerializer {
112154
});
113155
}
114156

115-
if (mapping.relationships) {
116-
Object.entries(mapping.relationships).forEach(([key, relationshipType]) => {
117-
const relationshipValue = (model as any)[key];
118-
if (relationshipValue && typeof relationshipType === 'string') {
119-
payload.data.relationships![key] = {
120-
data: {
121-
type: relationshipType,
122-
id: relationshipValue.id ?? relationshipValue,
123-
},
124-
};
125-
}
126-
});
127-
}
128-
129157
return payload;
130158
}
131159

0 commit comments

Comments
 (0)