Skip to content

Commit

Permalink
feat(api-client): invocation params no longer mandatory (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
patzick committed Oct 31, 2023
1 parent 87213fb commit 33d54db
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
17 changes: 17 additions & 0 deletions .changeset/pink-pianos-tickle.md
@@ -0,0 +1,17 @@
---
"@shopware/api-client": patch
---

`invoke` method parameters are no longer mandatory when no parameters are defined inside route.

Now instead of:

```ts
const result = await apiInstance.invoke("readContext get /context", {});
```

you can do:

```ts
const result = await apiInstance.invoke("readContext get /context");
```
6 changes: 4 additions & 2 deletions packages/api-client-next/src/index.ts
Expand Up @@ -116,11 +116,13 @@ export function createAPIClient<
: never,
>(
pathParam: INVOKE_PATH extends string ? INVOKE_PATH : never,
params: RequestParameters<OPERATION_NAME, OPERATIONS>,
...params: keyof RequestParameters<OPERATION_NAME, OPERATIONS> extends never
? [Record<PropertyKey, never>?]
: [RequestParameters<OPERATION_NAME, OPERATIONS>]
): Promise<RequestReturnType<OPERATION_NAME, OPERATIONS>> {
const [requestPath, options] = transformPathToQuery(
pathParam,
params as Record<string, string>,
params?.[0] as Record<string, string>,
);
// console.log("invoke with", requestPath, options);
return apiFetch<RequestReturnType<OPERATION_NAME, OPERATIONS>>(
Expand Down
16 changes: 13 additions & 3 deletions packages/api-client-next/src/invocations.e2e.test.ts
Expand Up @@ -16,7 +16,7 @@ describe("Test real API invocations", () => {
});
await expect(() =>
apiInstance.invoke("readCart get /checkout/cart?name", {
name: "qwe",
name: "myCartName",
}),
).rejects.toThrowErrorMatchingInlineSnapshot(
`
Expand All @@ -32,7 +32,7 @@ describe("Test real API invocations", () => {
accessToken,
contextToken: "",
});
const result = await apiInstance.invoke("readContext get /context", {});
const result = await apiInstance.invoke("readContext get /context");
expect(result).toHaveProperty("token");
expect(result.token).not.toBe("");
});
Expand All @@ -43,7 +43,7 @@ describe("Test real API invocations", () => {
accessToken,
contextToken: undefined,
});
const result = await apiInstance.invoke("readContext get /context", {});
const result = await apiInstance.invoke("readContext get /context");
expect(result).toHaveProperty("token");
expect(result.token).not.toBe("undefined");
});
Expand Down Expand Up @@ -95,4 +95,14 @@ describe("Test real API invocations", () => {
}
`);
});

it("should not require request parameters when operation does not have any", async () => {
const apiInstance = createAPIClient<operations, operationPaths>({
baseURL,
accessToken,
});
const result = await apiInstance.invoke("readContext get /context");
expect(result).toHaveProperty("token");
expect(result.token).not.toBe("undefined");
});
});
5 changes: 5 additions & 0 deletions packages/api-client-next/src/transformPathToQuery.ts
Expand Up @@ -54,6 +54,11 @@ export function transformPathToQuery<T extends Record<string, unknown>>(
query: Record<string, unknown>;
body?: Partial<T>;
};

if (!params) {
return [requestPathWithParams, returnOptions];
}

Object.keys(params).forEach((key) => {
if (
!pathParams.includes(key) &&
Expand Down

1 comment on commit 33d54db

@vercel
Copy link

@vercel vercel bot commented on 33d54db Oct 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.