Skip to content

Commit 251943b

Browse files
authored
Update Create and NestedCreate Service Hooks (#157)
* update create and nestedCreate service hooks * add changeset * update dependencies
1 parent 1b739ed commit 251943b

File tree

13 files changed

+583
-536
lines changed

13 files changed

+583
-536
lines changed

.changeset/mean-steaks-repair.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@rsm-hcd/javascript-react": minor
3+
---
4+
5+
Update create and nestedCreate service hooks to have optional record

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@
2323
},
2424
"devDependencies": {
2525
"@changesets/cli": "^2.27.9",
26-
"@dotenvx/dotenvx": "^1.16.0",
26+
"@dotenvx/dotenvx": "^1.20.1",
2727
"@rsm-hcd/tsconfig": "workspace:*",
2828
"husky": "^9.1.6",
2929
"lint-staged": "^15.2.10",
3030
"prettier": "^3.3.3",
31-
"turbo": "^2.1.3",
32-
"typedoc": "^0.26.8",
33-
"typescript": "^5.6.2"
31+
"turbo": "^2.2.3",
32+
"typedoc": "^0.26.10",
33+
"typescript": "^5.6.3"
3434
},
3535
"engines": {
3636
"node": ">=20",
3737
"pnpm": ">=9"
3838
},
39-
"packageManager": "pnpm@9.12.1"
39+
"packageManager": "pnpm@9.12.2"
4040
}

packages/eslint-config-custom/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"devDependencies": {
77
"@vercel/style-guide": "^5.2.0",
88
"eslint-config-turbo": "^1.13.4",
9-
"typescript": "^5.6.2"
9+
"typescript": "^5.6.3"
1010
}
1111
}

packages/javascript-core/package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@
3131
"@faker-js/faker": "^8.4.1",
3232
"@rsm-hcd/eslint-config-custom": "workspace:*",
3333
"@rsm-hcd/tsconfig": "workspace:*",
34-
"@swc/core": "^1.7.28",
34+
"@swc/core": "^1.7.39",
3535
"@types/humanize-plus": "^1.8.2",
36-
"@types/jest": "^29.5.13",
37-
"@types/lodash": "^4.17.10",
38-
"@types/node": "^20.16.10",
36+
"@types/jest": "^29.5.14",
37+
"@types/lodash": "^4.17.12",
38+
"@types/node": "^20.17.0",
3939
"@types/rosie": "^0.0.45",
4040
"axios": "^1.7.7",
4141
"cross-env": "^7.0.3",
4242
"humanize-plus": "^1.8.2",
43-
"i18next": "^23.15.2",
43+
"i18next": "^23.16.3",
4444
"i18next-browser-languagedetector": "^8.0.0",
4545
"immutable": "^4.3.7",
4646
"jest": "^29.7.0",
@@ -54,7 +54,7 @@
5454
"ts-jest": "^29.2.5",
5555
"ts-node": "^10.9.2",
5656
"tsup": "^8.3.0",
57-
"typescript": "^5.6.2"
57+
"typescript": "^5.6.3"
5858
},
5959
"files": [
6060
"dist/**"

packages/javascript-react/package.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,29 @@
3232
"@rsm-hcd/javascript-core": "workspace:*",
3333
"@rsm-hcd/javascript-testing": "workspace:*",
3434
"@rsm-hcd/tsconfig": "workspace:*",
35-
"@swc/core": "^1.7.28",
35+
"@swc/core": "^1.7.39",
3636
"@swc/jest": "^0.2.36",
37-
"@testing-library/jest-dom": "^6.5.0",
37+
"@testing-library/jest-dom": "^6.6.2",
3838
"@testing-library/react": "^16.0.1",
3939
"@testing-library/react-hooks": "^8.0.1",
40-
"@types/jest": "^29.5.13",
40+
"@types/jest": "^29.5.14",
4141
"@types/node": "20.11.20",
42-
"@types/react": "^18.3.11",
43-
"@types/react-dom": "^18.3.0",
42+
"@types/react": "^18.3.12",
43+
"@types/react-dom": "^18.3.1",
4444
"@types/rosie": "0.0.45",
4545
"axios": "^1.7.7",
4646
"cross-env": "^7.0.3",
4747
"eslint-plugin-react-hooks": "^4.6.2",
4848
"humanize-plus": "1.8.2",
49-
"i18next": "^23.15.2",
49+
"i18next": "^23.16.3",
5050
"i18next-browser-languagedetector": "^8.0.0",
5151
"immutable": "^4.3.7",
5252
"jest": "^29.7.0",
5353
"jest-environment-jsdom": "^29.7.0",
5454
"jest-extended": "^4.0.2",
5555
"jest-fixed-jsdom": "^0.0.4",
5656
"lodash": "4.17.21",
57-
"msw": "^2.4.9",
57+
"msw": "^2.5.1",
5858
"prettier": "^3.3.3",
5959
"react": "^18.3.1",
6060
"react-dom": "^18.3.1",
@@ -64,7 +64,7 @@
6464
"rosie": "^2.1.1",
6565
"ts-node": "^10.9.2",
6666
"tsup": "^8.3.0",
67-
"typescript": "^5.6.2",
67+
"typescript": "^5.6.3",
6868
"undici": "~5.28.4",
6969
"window-resizeto": "0.0.2"
7070
},

packages/javascript-react/src/services/service-factory.test.tsx

+34-22
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,22 @@ describe("ServiceFactory", () => {
112112
describe("create", () => {
113113
itReturnsFunction(ServiceFactory.create, baseEndpoint);
114114

115-
it("given null, throws error", async () => {
116-
expect.assertions(1);
117-
try {
118-
const sut = ServiceFactory.create(
119-
StubResourceRecord,
120-
baseEndpoint
121-
);
122-
await sut(null as any); // <----- passing null
123-
} catch (e) {
124-
expect(e).toBeInstanceOf(TypeError);
125-
}
115+
it("given no arguments, throws error", async () => {
116+
// Arrange
117+
const expected = Factory.build<StubResourceRecord>(
118+
FactoryType.StubResourceRecord
119+
);
120+
121+
const sut = ServiceFactory.create(StubResourceRecord, baseEndpoint);
122+
mockPostSuccess(expected);
123+
124+
// Act
125+
const response = await sut();
126+
127+
// Assert
128+
expect(response.resultObject).not.toBeNull();
129+
expect(response.resultObject).toBeInstanceOf(StubResourceRecord);
130+
expect(response.resultObject!.name).toEqual(expected.name);
126131
});
127132

128133
it("when successful, returns response mapped to supplied TRecord", async () => {
@@ -310,17 +315,24 @@ describe("ServiceFactory", () => {
310315
itReturnsFunction(ServiceFactory.nestedCreate, nestedBaseEndpoint);
311316

312317
it("given null, throws error", async () => {
313-
expect.assertions(1);
314-
try {
315-
const sut = ServiceFactory.nestedCreate(
316-
StubResourceRecord,
317-
baseEndpoint
318-
);
319-
320-
await sut(null as any, {}); // <----- passing null
321-
} catch (e) {
322-
expect(e).toBeInstanceOf(TypeError);
323-
}
318+
// Arrange
319+
const expected = Factory.build<StubResourceRecord>(
320+
FactoryType.StubResourceRecord
321+
);
322+
323+
const sut = ServiceFactory.nestedCreate(
324+
StubResourceRecord,
325+
baseEndpoint
326+
);
327+
mockPostSuccess(expected);
328+
329+
// Act
330+
const response = await sut(null, {});
331+
332+
// Assert
333+
expect(response.resultObject).not.toBeNull();
334+
expect(response.resultObject).toBeInstanceOf(StubResourceRecord);
335+
expect(response.resultObject!.name).toEqual(expected.name);
324336
});
325337

326338
it("when successful, returns response mapped to supplied TRecord", async () => {

packages/javascript-react/src/services/service-factory.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ const ServiceFactory = {
126126
baseEndpoint: string
127127
): NestedCreateServiceWithSignal<TRecord, TPathParams> {
128128
return (
129-
record: TRecord,
129+
record: TRecord | undefined | null,
130130
pathParams: TPathParams,
131131
signal?: AbortSignal
132132
) => {
@@ -232,17 +232,13 @@ const _bulkUpdate = async function <
232232
const _create = async function <TRecord extends RecordType>(
233233
recordType: new () => TRecord,
234234
url: string,
235-
record?: TRecord,
235+
record?: TRecord | null,
236236
signal?: AbortSignal
237237
) {
238-
if (!record) {
239-
throw new TypeError(
240-
`Could not create resource. Record value was invalid.`
241-
);
242-
}
238+
const requestData = record != null ? record.toJS() : null;
243239

244240
return axios
245-
.post(url, record.toJS(), { signal })
241+
.post(url, requestData, { signal })
246242
.then((r) => ServiceUtils.mapAxiosResponse(recordType, r));
247243
};
248244

packages/javascript-react/src/services/service-hook-factory.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const ServiceHookFactory = {
8383

8484
const create = useCallback(
8585
async function create(
86-
record: TRecord
86+
record?: TRecord
8787
): Promise<ServiceResponse<TRecord>> {
8888
try {
8989
return await serviceCreate(record, signal);
@@ -244,7 +244,7 @@ const ServiceHookFactory = {
244244

245245
const create = useCallback(
246246
async function create(
247-
record: TRecord,
247+
record: TRecord | null | undefined,
248248
pathParams: TPathParams
249249
): Promise<ServiceResponse<TRecord>> {
250250
try {

packages/javascript-react/src/types/create-service-type.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { ServiceResponse } from "@rsm-hcd/javascript-core";
66
* @param record Record to be created
77
*/
88
export type CreateService<TRecord> = (
9-
record: TRecord
9+
record?: TRecord
1010
) => Promise<ServiceResponse<TRecord>>;
1111

1212
/**
@@ -15,6 +15,6 @@ export type CreateService<TRecord> = (
1515
* @param record Record to be created
1616
*/
1717
export type CreateServiceWithSignal<TRecord> = (
18-
record: TRecord,
18+
record?: TRecord,
1919
signal?: AbortSignal
2020
) => Promise<ServiceResponse<TRecord>>;

packages/javascript-react/src/types/nested-create-service-type.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { ServiceResponse } from "@rsm-hcd/javascript-core";
77
* @param pathParams Object with path parameters to be replaced in the endpoint route
88
*/
99
export type NestedCreateService<TRecord, TPathParams> = (
10-
record: TRecord,
10+
record: TRecord | null | undefined,
1111
pathParams: TPathParams
1212
) => Promise<ServiceResponse<TRecord>>;
1313

@@ -19,7 +19,7 @@ export type NestedCreateService<TRecord, TPathParams> = (
1919
* @param signal AbortSignal to be used for aborting the request
2020
*/
2121
export type NestedCreateServiceWithSignal<TRecord, TPathParams> = (
22-
record: TRecord,
22+
record: TRecord | null | undefined,
2323
pathParams: TPathParams,
2424
signal?: AbortSignal
2525
) => Promise<ServiceResponse<TRecord>>;

packages/javascript-testing/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
"devDependencies": {
3434
"@rsm-hcd/eslint-config-custom": "workspace:*",
3535
"@rsm-hcd/tsconfig": "workspace:*",
36-
"@swc/core": "^1.7.28",
37-
"@types/jest": "^29.5.13",
36+
"@swc/core": "^1.7.39",
37+
"@types/jest": "^29.5.14",
3838
"@types/rosie": "^0.0.45",
3939
"cross-env": "^7.0.3",
4040
"jest-environment-jsdom": "^29.7.0",
@@ -44,7 +44,7 @@
4444
"ts-jest": "^29.2.5",
4545
"ts-node": "^10.9.2",
4646
"tsup": "^8.3.0",
47-
"typescript": "^5.6.2"
47+
"typescript": "^5.6.3"
4848
},
4949
"files": [
5050
"dist/**"

0 commit comments

Comments
 (0)