Skip to content

Commit

Permalink
Fix/upload json file (#554)
Browse files Browse the repository at this point in the history
* chore: Upgrade superagent

* chore: Upgrade superagent's type

* fix: Upload JSON file
  • Loading branch information
rrr523 authored May 31, 2024
1 parent 09ed37d commit 64a62b1
Show file tree
Hide file tree
Showing 4 changed files with 15,104 additions and 9,960 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.
5 changes: 3 additions & 2 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 All @@ -82,7 +83,7 @@
"long": "^5.2.1",
"mime-types": "^2.1.35",
"reflect-metadata": "^0.1.13",
"superagent": "^8.1.2",
"superagent": "^9.0.2",
"tsyringe": "^4.8.0"
},
"devDependencies": {
Expand All @@ -96,7 +97,7 @@
"@types/lodash.sortby": "^4.7.7",
"@types/mime": "^3.0.1",
"@types/mime-types": "^2.1.4",
"@types/superagent": "^8.1.6",
"@types/superagent": "^8.1.7",
"@types/xml2js": "^0.4.11",
"dotenv": "^16.0.3",
"jest": "^29.5.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
Loading

0 comments on commit 64a62b1

Please sign in to comment.