Skip to content

Commit

Permalink
Merge branch 'main' into chore/disable-eslint-for-nextjs-public-dir
Browse files Browse the repository at this point in the history
  • Loading branch information
lsliwaradioluz authored Nov 26, 2024
2 parents 638284b + 9b8d334 commit 0661a24
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/content/4.sdk/4.api/sdk.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@
},
{
"kind": "Content",
"text": "(options?: Options, parent?: {\n methods: ExtendedModule[\"connector\"];\n context?: ExtendedModule[\"context\"];\n}) => InitializedExtension"
"text": "(options: Options, parent: {\n methods: ExtendedModule[\"connector\"];\n context: ExtendedModule[\"context\"];\n}) => InitializedExtension"
},
{
"kind": "Content",
Expand Down
9 changes: 8 additions & 1 deletion docs/content/4.sdk/5.reference/sdk-change-log.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change log

## 3.4.1

### Patch Changes

- **[FIXED]** Fixed type inference for function-based configurations when using the [extend](https://docs.alokai.com/sdk/advanced/extending-module#extend) method. Now the `methods` object is correctly typed.
- **[FIXED]** Fixed multiple files upload, now it works as expected.

## 3.4.0

### Minor Changes
Expand All @@ -12,7 +19,7 @@
```typescript
// Upload a file using multipart/form-data
await sdk.commerce.uploadFile(
{ file: new File(["content"], "test.txt", { type: "text/plain" }) },
{ file: new File(["content"], "test.pdf", { type: "application/pdf" }) },
prepareConfig({
headers: {
"Content-Type": "multipart/form-data",
Expand Down
7 changes: 7 additions & 0 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change log

## 3.4.1

### Patch Changes

- **[FIX]** Fixed type inference for function-based configurations when using the [extend](https://docs.alokai.com/sdk/advanced/extending-module#extend) method. Now the `methods` object is correctly typed.
- **[FIXED]** Fixed multiple files upload, now it works as expected.

## 3.4.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@vue-storefront/sdk",
"license": "MIT",
"version": "3.4.0",
"version": "3.4.1",
"main": "lib/index.cjs.js",
"module": "lib/index.es.js",
"types": "lib/index.d.ts",
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/__tests__/__mocks__/apiClient/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type Endpoints = {
/**
* Upload a file.
*/
uploadFile: (params: {
file: { name: string; content: string };
}) => Promise<{ file: { name: string; content: string } }>;
uploadFile: (
params: any
) => Promise<{ file: { name: string; content: string } }>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -919,4 +919,43 @@ describe("middlewareModule", () => {
expect.any(Object)
);
});

it("should handle multiple files in FormData", async () => {
const customHttpClient = jest.fn();
const sdkConfig = {
commerce: buildModule(middlewareModule<Endpoints>, {
apiUrl: "http://localhost:8181/commerce",
httpClient: customHttpClient,
}),
};
const sdk = initSDK(sdkConfig);

const file1 = { name: "test1.txt", content: "test" };
const file2 = { name: "test2.txt", content: "test" };

await sdk.commerce.uploadFile(
{
files: [file1, file2],
metadata: {
description: "Multiple files upload",
},
},
prepareConfig({
headers: {
"Content-Type": "multipart/form-data",
},
})
);

// Verify the call was made with correct URL and config
const [url, params, config] = customHttpClient.mock.calls[0];
expect(url).toBe("http://localhost:8181/commerce/uploadFile");
expect(params[0]).toEqual({
files: [file1, file2],
metadata: {
description: "Multiple files upload",
},
});
expect(config.headers["Content-Type"]).toBe("multipart/form-data");
});
});
2 changes: 1 addition & 1 deletion packages/sdk/src/modules/buildModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function buildModule<

const resolvedExtension =
typeof extension === "function"
? extension(extensionOptions, {
? extension(extensionOptions ?? ({} as ExtensionOptions), {
methods: resolvedModule.connector,
context: resolvedModule?.context ?? {},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ export const getRequestSender = (options: Options): RequestSender => {
return logger;
};

const appendToFormData = (formData: FormData, key: string, value: any) => {
if (value instanceof Blob || value instanceof File) {
formData.append(key, value);
} else if (typeof value === "object" && value !== null) {
Object.entries(value).forEach(([nestedKey, nestedValue]) => {
appendToFormData(formData, nestedKey, nestedValue);
});
} else {
formData.append(key, JSON.stringify(value));
}
};

const defaultHTTPClient: HTTPClient = async (url, params, config) => {
const isMultipart = config?.headers?.["Content-Type"]?.includes(
"multipart/form-data"
Expand All @@ -118,11 +130,7 @@ export const getRequestSender = (options: Options): RequestSender => {
} else if (isMultipart) {
const formData = new FormData();
Object.entries(params).forEach(([key, value]) => {
if (value instanceof Blob || value instanceof File) {
formData.append(key, value);
} else {
formData.append(key, JSON.stringify(value));
}
appendToFormData(formData, key, value);
});
body = formData;

Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,10 @@ export type ExtensionInitializer<
InitializedExtension extends Extension<ExtendedModule>,
Options extends ModuleOptions
> = (
options?: Options,
parent?: {
options: Options,
parent: {
methods: ExtendedModule["connector"];
context?: ExtendedModule["context"];
context: ExtendedModule["context"];
}
) => InitializedExtension;

Expand Down

0 comments on commit 0661a24

Please sign in to comment.