Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Convert enum to number #287

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/flat-cameras-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-js-sdk': patch
---

feat: convert XML enum to number
1 change: 1 addition & 0 deletions packages/chain-sdk/src/api/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ export class Bucket implements IBucket {
readQuota: Number(res.GetReadQuotaResult.ReadQuotaSize ?? '0'),
freeQuota: Number(res.GetReadQuotaResult.SPFreeReadQuotaSize ?? '0'),
consumedQuota: Number(res.GetReadQuotaResult.ReadConsumedSize ?? '0'),
freeConsumedSize: Number(res.GetReadQuotaResult.FreeConsumedSize ?? '0'),
},
message: 'Get bucket read quota.',
statusCode: result.status,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GetBucketMetaResponse } from '@/types';
import { formatBucketInfo } from '@/types/sp-xml/Common';
import { XMLParser } from 'fast-xml-parser';

// https://docs.bnbchain.org/greenfield-docs/docs/api/storgae-provider-rest/get_bucket_meta
Expand All @@ -8,5 +9,13 @@ export const parseGetBucketMetaResponse = async (data: string) => {
});
const res = xmlParser.parse(data) as GetBucketMetaResponse;

res.GfSpGetBucketMetaResponse.Bucket = {
...res.GfSpGetBucketMetaResponse.Bucket,
BucketInfo: formatBucketInfo(res.GfSpGetBucketMetaResponse.Bucket.BucketInfo),
DeleteAt: Number(res.GfSpGetBucketMetaResponse.Bucket.DeleteAt),
UpdateAt: Number(res.GfSpGetBucketMetaResponse.Bucket.UpdateAt),
UpdateTime: Number(res.GfSpGetBucketMetaResponse.Bucket.UpdateTime),
};

return res;
};
12 changes: 2 additions & 10 deletions packages/chain-sdk/src/clients/spclient/spApis/getUserBuckets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GetUserBucketsResponse } from '@/types';
import { convertStrToBool } from '@/types/sp-xml/Common';
import { convertStrToBool, formatBucketInfo } from '@/types/sp-xml/Common';
import { XMLParser } from 'fast-xml-parser';

// https://docs.bnbchain.org/greenfield-docs/docs/api/storgae-provider-rest/get_user_buckets
Expand All @@ -16,17 +16,9 @@ export const parseGetUserBucketsResponse = async (data: string) => {
}

Buckets = Buckets.map((item) => {
const BucketInfo = {
...item.BucketInfo,
CreateAt: Number(item.BucketInfo.CreateAt),
// PrimarySpId: Number(item.BucketInfo.PrimarySpId),
GlobalVirtualGroupFamilyId: Number(item.BucketInfo.GlobalVirtualGroupFamilyId),
ChargedReadQuota: Number(item.BucketInfo.ChargedReadQuota),
};

return {
...item,
BucketInfo,
BucketInfo: formatBucketInfo(item.BucketInfo),
// @ts-ignore
Removed: convertStrToBool(item.Removed),
DeleteAt: Number(item.DeleteAt),
Expand Down
42 changes: 21 additions & 21 deletions packages/chain-sdk/src/tests/parsexml.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ describe('parseListObjectsByBucketNameResponse', () => {
Id: 739112,
LocalVirtualGroupId: 1,
PayloadSize: 205,
Visibility: '1',
Visibility: 1,
ContentType: 'application/x-yaml',
CreateAt: 1692971564,
ObjectStatus: '1',
RedundancyType: '0',
SourceType: '0',
ObjectStatus: 1,
RedundancyType: 0,
SourceType: 0,
Checksums: [
'a7554caa696d4db8c8ea4500ad2eac6244bca79e3c3193db8cc699a7e89cf341',
'b054fd20d637bfa706182288d687f2c0f0ef5afaa4114170bc18699b268e9c28',
Expand Down Expand Up @@ -247,12 +247,12 @@ describe('parseListObjectsByBucketNameResponse', () => {
Id: 739112,
LocalVirtualGroupId: 1,
PayloadSize: 205,
Visibility: '1',
Visibility: 1,
ContentType: 'application/x-yaml',
CreateAt: 1692971564,
ObjectStatus: '1',
RedundancyType: '0',
SourceType: '0',
ObjectStatus: 1,
RedundancyType: 0,
SourceType: 0,
Checksums: [
'a7554caa696d4db8c8ea4500ad2eac6244bca79e3c3193db8cc699a7e89cf341',
'b054fd20d637bfa706182288d687f2c0f0ef5afaa4114170bc18699b268e9c28',
Expand All @@ -277,12 +277,12 @@ describe('parseListObjectsByBucketNameResponse', () => {
Id: 739112,
LocalVirtualGroupId: 1,
PayloadSize: 205,
Visibility: '1',
Visibility: 1,
ContentType: 'application/x-yaml',
CreateAt: 1692971564,
ObjectStatus: '1',
RedundancyType: '0',
SourceType: '0',
ObjectStatus: 1,
RedundancyType: 0,
SourceType: 0,
Checksums: [
'a7554caa696d4db8c8ea4500ad2eac6244bca79e3c3193db8cc699a7e89cf341',
'b054fd20d637bfa706182288d687f2c0f0ef5afaa4114170bc18699b268e9c28',
Expand Down Expand Up @@ -358,14 +358,14 @@ describe('parseGetUserBucketsResponse', () => {
BucketInfo: {
Owner: '0x1C893441AB6c1A75E01887087ea508bE8e07AAae',
BucketName: 'false',
Visibility: '1',
Visibility: 1,
Id: '1156769',
SourceType: '0',
SourceType: 0,
CreateAt: 1693279149,
PaymentAddress: '0x1C893441AB6c1A75E01887087ea508bE8e07AAae',
GlobalVirtualGroupFamilyId: 8,
ChargedReadQuota: 0,
BucketStatus: '0',
BucketStatus: 0,
},
Removed: false,
DeleteAt: 0,
Expand All @@ -380,14 +380,14 @@ describe('parseGetUserBucketsResponse', () => {
BucketInfo: {
Owner: '0x1C893441AB6c1A75E01887087ea508bE8e07AAae',
BucketName: 'false',
Visibility: '1',
Visibility: 1,
Id: '1156769',
SourceType: '0',
SourceType: 0,
CreateAt: 1693279149,
PaymentAddress: '0x1C893441AB6c1A75E01887087ea508bE8e07AAae',
GlobalVirtualGroupFamilyId: 8,
ChargedReadQuota: 0,
BucketStatus: '0',
BucketStatus: 0,
},
Removed: false,
DeleteAt: 0,
Expand Down Expand Up @@ -434,14 +434,14 @@ describe('parseGetUserBucketsResponse', () => {
BucketInfo: {
Owner: '0x1C893441AB6c1A75E01887087ea508bE8e07AAae',
BucketName: 'false',
Visibility: '1',
Visibility: 1,
Id: '1156769',
SourceType: '0',
SourceType: 0,
CreateAt: 1693279149,
PaymentAddress: '0x1C893441AB6c1A75E01887087ea508bE8e07AAae',
GlobalVirtualGroupFamilyId: 8,
ChargedReadQuota: 0,
BucketStatus: '0',
BucketStatus: 0,
},
Removed: false,
DeleteAt: 0,
Expand Down
63 changes: 36 additions & 27 deletions packages/chain-sdk/src/types/sp-xml/Common.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import {
BucketStatus,
ObjectStatus,
RedundancyType,
SourceType,
VisibilityType,
} from '@bnb-chain/greenfield-cosmos-types/greenfield/storage/common';

export interface BucketMeta {
BucketInfo: BucketInfo;
Removed: boolean;
Expand All @@ -19,17 +11,17 @@ export interface BucketMeta {
}

export interface BucketInfo {
Owner: string;
// PrimarySpId: number;
BucketName: string;
Visibility: keyof typeof VisibilityType;
Id: string;
SourceType: keyof typeof SourceType;
BucketStatus: number;
ChargedReadQuota: number;
CreateAt: number;
PaymentAddress: string;
// PrimarySpId: number;
GlobalVirtualGroupFamilyId: number;
ChargedReadQuota: number;
BucketStatus: keyof typeof BucketStatus;
Id: string;
Owner: string;
PaymentAddress: string;
SourceType: number;
Visibility: number;
}

export interface StreamRecord {
Expand Down Expand Up @@ -59,29 +51,46 @@ export interface ObjectMeta {
}

export interface ObjectInfo {
Owner: string;
Creator: string;
BucketName: string;
ObjectName: string;
Checksums: string[];
ContentType: string;
CreateAt: number;
Creator: string;
Id: number;
LocalVirtualGroupId: number;
ObjectName: string;
ObjectStatus: number;
Owner: string;
PayloadSize: number;
Visibility: keyof typeof VisibilityType;
ContentType: string;
CreateAt: number;
ObjectStatus: keyof typeof ObjectStatus;
RedundancyType: keyof typeof RedundancyType;
SourceType: keyof typeof SourceType;
Checksums: string[];
RedundancyType: number;
SourceType: number;
Visibility: number;
}

export function formatBucketInfo(o: BucketInfo) {
return {
...o,
// PrimarySpId: Number(item.BucketInfo.PrimarySpId),
BucketStatus: Number(o.BucketStatus),
ChargedReadQuota: Number(o.ChargedReadQuota),
CreateAt: Number(o.CreateAt),
GlobalVirtualGroupFamilyId: Number(o.GlobalVirtualGroupFamilyId),
SourceType: Number(o.SourceType),
Visibility: Number(o.Visibility),
};
}

export function formatObjectInfo(o: ObjectInfo) {
return {
...o,
CreateAt: Number(o.CreateAt),
Id: Number(o.Id),
LocalVirtualGroupId: Number(o.LocalVirtualGroupId),
ObjectStatus: Number(o.ObjectStatus),
PayloadSize: Number(o.PayloadSize),
CreateAt: Number(o.CreateAt),
RedundancyType: Number(o.RedundancyType),
SourceType: Number(o.SourceType),
Visibility: Number(o.Visibility),
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/chain-sdk/src/types/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export interface IQuotaProps {
readQuota: number;
freeQuota: number;
consumedQuota: number;
freeConsumedSize: number;
}

export type TBaseGetCreateObject = {
Expand Down