Skip to content

Commit

Permalink
fix: Upload JSON file
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 committed May 31, 2024
1 parent beffb64 commit d44843d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-news-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-js-sdk': patch
---

fix: Upload file that `content-type` is `application/json`, cancel serialize.
1 change: 1 addition & 0 deletions packages/js-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"@ethersproject/units": "^5.7.0",
"@metamask/eth-sig-util": "^5.0.2",
"@noble/curves": "^1.3.0",
"browser-or-node": "^3.0.0",
"cross-fetch": "^4.0.0",
"dayjs": "^1.11.7",
"ethereum-cryptography": "^2.0.0",
Expand Down
13 changes: 7 additions & 6 deletions packages/js-sdk/src/clients/spclient/spClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { getGetObjectMetaInfo } from './spApis/getObject';
import { getPutObjectMetaInfo } from './spApis/putObject';
import { assertFileType, assertHttpMethod } from '@/utils';
import { UploadFile } from '@/types/sp/Common';
import { isNode } from 'browser-or-node';

export interface ISpClient {
callApi(
Expand Down Expand Up @@ -168,6 +169,7 @@ export class SpClient implements ISpClient {
},
) {
const R = superagent.put(url);
R.buffer(true);
R.timeout(timeout);
R.ok((res) => res.status < 500);

Expand All @@ -178,11 +180,6 @@ export class SpClient implements ISpClient {
}

try {
const R = superagent.put(url);
R.buffer(true);
R.timeout(timeout);
R.ok((res) => res.status < 500);

if (options.headers) {
(options.headers as Headers).forEach((v: string, k: string) => {
R.set(k, v);
Expand All @@ -196,7 +193,11 @@ export class SpClient implements ISpClient {
}

const file = assertFileType(uploadFile) ? uploadFile.content : uploadFile;
const response = await R.send(file);

// https://ladjs.github.io/superagent/docs/index.html#serializing-request-body
const serializeFile =
isNode && R.get('Content-Type') === 'application/json' ? file.toString() : file;
const response = await R.send(serializeFile);
const { status } = response;

if (status === SP_NOT_AVAILABLE_ERROR_CODE) {
Expand Down
12 changes: 10 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d44843d

Please sign in to comment.