Skip to content

Commit

Permalink
fix: Delegated create folder response return parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 committed Apr 9, 2024
1 parent acf8644 commit cb9658e
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-tomatoes-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-js-sdk': patch
---

fix: Delegated Create Folder APi return parse error message
2 changes: 2 additions & 0 deletions examples/nextjs/src/components/object/create/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ export const CreateObject = () => {
},
);

console.log('res', res);

if (res.code === 0) {
alert('success');
}
Expand Down
29 changes: 18 additions & 11 deletions packages/js-sdk/src/api/objects.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { getDelegatedCreateFolderMetaInfo } from '@/clients/spclient/spApis/delegatedCreateFolder';
import {
getDelegatedCreateFolderMetaInfo,
parseDelegatedCreateFolderResponse,
} from '@/clients/spclient/spApis/delegatedCreateFolder';
import {
getObjectOffsetInfo,
parseObjectOffsetResponse,
Expand All @@ -8,6 +11,12 @@ import {
parseObjectStatusResponse,
} from '@/clients/spclient/spApis/getObjectStatus';
import { getResumablePutObjectMetaInfo } from '@/clients/spclient/spApis/resumablePutObject';
import { DelegatedOpts } from '@/types/sp/Common';
import {
DelegateCreateFolderRepsonse,
DelegatedCreateFolderRequest,
} from '@/types/sp/DelegateCreateFolder';
import { DelegatedPubObjectRequest } from '@/types/sp/DelegatedPubObject';
import { UploadProgressResponse } from '@/types/sp/UploadProgress';
import { assertAuthType, assertStringRequire } from '@/utils/asserts/params';
import {
Expand Down Expand Up @@ -92,12 +101,7 @@ import {
import { GetObjectRequest } from '../types/sp/GetObject';
import { GetObjectMetaRequest, GetObjectMetaResponse } from '../types/sp/GetObjectMeta';
import { ListObjectsByBucketNameResponse } from '../types/sp/ListObjectsByBucketName';
import {
DelegatedCreateFolderRequest,
DelegatedOpts,
DelegatedPubObjectRequest,
PutObjectRequest,
} from '../types/sp/PutObject';
import { PutObjectRequest } from '../types/sp/PutObject';
import {
checkObjectName,
generateUrlByBucketName,
Expand Down Expand Up @@ -153,7 +157,7 @@ export interface IObject {
delegateCreateFolder(
params: DelegatedCreateFolderRequest,
authType: AuthType,
): Promise<SpResponse<null>>;
): Promise<SpResponse<DelegateCreateFolderRepsonse>>;

putObjectPolicy(
bucketName: string,
Expand Down Expand Up @@ -396,7 +400,7 @@ export class Objects implements IObject {
return { code: 0, message: 'Put object success.', statusCode: status };
} catch (error: any) {
return {
code: -1,
code: error.code || -1,
message: error.message,
statusCode: error?.statusCode || NORMAL_ERROR_CODE,
};
Expand Down Expand Up @@ -924,10 +928,13 @@ export class Objects implements IObject {
);
const { status } = result;

return { code: 0, message: 'Put object success.', statusCode: status };
const xmlData = await result.text();
const res = parseDelegatedCreateFolderResponse(xmlData);

return { code: 0, message: 'Create folder success.', statusCode: status, body: res };
} catch (error: any) {
return {
code: -1,
code: error.code || -1,
message: error.message,
statusCode: error?.statusCode || NORMAL_ERROR_CODE,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { EMPTY_STRING_SHA256, METHOD_POST } from '@/constants';
import { ReqMeta } from '@/types';
import { DelegateCreateFolderRepsonse } from '@/types/sp/DelegateCreateFolder';
import { generateUrlByBucketName } from '@/utils';
import { VisibilityType } from '@bnb-chain/greenfield-cosmos-types/greenfield/storage/common';
import { XMLParser } from 'fast-xml-parser';
import { encodePath, getSortQueryParams } from '../auth';

export const getDelegatedCreateFolderMetaInfo = async (
Expand Down Expand Up @@ -50,3 +52,12 @@ export const getDelegatedCreateFolderMetaInfo = async (
reqMeta,
};
};

export const parseDelegatedCreateFolderResponse = (data: string) => {
const xmlParser = new XMLParser({
parseTagValue: false,
});

const res = xmlParser.parse(data) as DelegateCreateFolderRepsonse;
return res;
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EMPTY_STRING_SHA256, METHOD_POST } from '@/constants';
import { DelegatedOpts, ReqMeta } from '@/types';
import { ReqMeta } from '@/types';
import { DelegatedOpts } from '@/types/sp/Common';
import { generateUrlByBucketName } from '@/utils/asserts/s3';
import { encodePath, getSortQueryParams } from '../auth';

Expand Down
3 changes: 2 additions & 1 deletion packages/js-sdk/src/clients/spclient/spClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class SpClient implements ISpClient {
if (!response.ok) {
const xmlError = await response.text();
const { code, message } = await parseError(xmlError);

throw {
code: code || customError?.code,
message: message || customError?.message,
Expand All @@ -88,7 +89,7 @@ export class SpClient implements ISpClient {

return response;
} catch (error) {
return Promise.reject(error + ': timeout, try to increase the request time');
return Promise.reject(error);
}
}

Expand Down
12 changes: 12 additions & 0 deletions packages/js-sdk/src/types/sp/Common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { VisibilityType } from '../common';

export type BucketMeta = {
/**
* defines the information of the bucket.
Expand Down Expand Up @@ -322,3 +324,13 @@ export interface PolicyMeta {
*/
ExpirationTime: number;
}

export type DelegatedOpts = {
visibility: VisibilityType;
isUpdate?: boolean;
};

export type ResumableOpts = {
disableResumable: boolean;
partSize?: number;
};
15 changes: 15 additions & 0 deletions packages/js-sdk/src/types/sp/DelegateCreateFolder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { DelegatedOpts } from './Common';

export type DelegatedCreateFolderRequest = {
bucketName: string;
objectName: string;
delegatedOpts: DelegatedOpts;
endpoint?: string;
timeout?: number;
};

export interface DelegateCreateFolderRepsonse {
CreateHash: {
TxHash: string;
};
}
12 changes: 12 additions & 0 deletions packages/js-sdk/src/types/sp/DelegatedPubObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { DelegatedOpts, ResumableOpts } from './Common';

export type DelegatedPubObjectRequest = {
bucketName: string;
objectName: string;
body: File;
delegatedOpts: DelegatedOpts;
endpoint?: string;
timeout?: number;
contentType?: string;
resumableOpts?: ResumableOpts;
};
31 changes: 1 addition & 30 deletions packages/js-sdk/src/types/sp/PutObject.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { VisibilityType } from '../common';
import { ResumableOpts } from './Common';

export type PutObjectRequest = {
bucketName: string;
Expand All @@ -9,32 +9,3 @@ export type PutObjectRequest = {
endpoint?: string;
resumableOpts?: ResumableOpts;
};

export type DelegatedPubObjectRequest = {
bucketName: string;
objectName: string;
body: File;
delegatedOpts: DelegatedOpts;
endpoint?: string;
timeout?: number;
contentType?: string;
resumableOpts?: ResumableOpts;
};

export type DelegatedCreateFolderRequest = {
bucketName: string;
objectName: string;
delegatedOpts: DelegatedOpts;
endpoint?: string;
timeout?: number;
};

export type DelegatedOpts = {
visibility: VisibilityType;
isUpdate?: boolean;
};

export type ResumableOpts = {
disableResumable: boolean;
partSize?: number;
};
2 changes: 2 additions & 0 deletions packages/js-sdk/src/types/sp/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './DelegateCreateFolder';
export * from './DelegatedPubObject';
export * from './ErrorResponse';
export * from './GetBucketMeta';
export * from './GetObject';
Expand Down

0 comments on commit cb9658e

Please sign in to comment.