-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: XML type align to go struct (#272)
- Loading branch information
Showing
22 changed files
with
236 additions
and
279 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@bnb-chain/greenfield-js-sdk': patch | ||
--- | ||
|
||
fix: XML response from sp align to go struct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 8 additions & 16 deletions
24
packages/chain-sdk/src/clients/spclient/spApis/getBucketMeta.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,14 @@ | ||
import { GetBucketMetaResponse } from '@/types'; | ||
import { XMLParser } from 'fast-xml-parser'; | ||
import xml from 'xml2js'; | ||
|
||
// https://docs.bnbchain.org/greenfield-docs/docs/api/storgae-provider-rest/get_bucket_meta | ||
export const parseGetBucketMetaResponse = (data: string) => { | ||
const xmlParser = new XMLParser({ | ||
isArray: (tagName: string) => { | ||
if (tagName === 'Buckets') return true; | ||
return false; | ||
}, | ||
numberParseOptions: { | ||
hex: false, | ||
leadingZeros: true, | ||
skipLike: undefined, | ||
eNotation: false, | ||
}, | ||
}); | ||
|
||
const res = xmlParser.parse(data) as GetBucketMetaResponse; | ||
export const parseGetBucketMetaResponse = async (data: string) => { | ||
// Buckets | ||
const res = (await xml.parseStringPromise(data, { | ||
strict: true, | ||
explicitRoot: true, | ||
explicitArray: false, | ||
})) as GetBucketMetaResponse; | ||
|
||
return res; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 23 additions & 15 deletions
38
packages/chain-sdk/src/clients/spclient/spApis/getObjectMeta.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,28 @@ | ||
import { formatObjectInfo } from '@/types'; | ||
import { GetObjectMetaResponse } from '@/types/sp-xml/GetObjectMetaResponse'; | ||
import { XMLParser } from 'fast-xml-parser'; | ||
import xml from 'xml2js'; | ||
|
||
// https://docs.bnbchain.org/greenfield-docs/docs/api/storgae-provider-rest/get_object_meta | ||
export const parseGetObjectMetaResponse = (data: string) => { | ||
const xmlParser = new XMLParser({ | ||
isArray: (tagName: string) => { | ||
if (tagName === 'Objects') return true; | ||
return false; | ||
}, | ||
numberParseOptions: { | ||
hex: false, | ||
leadingZeros: true, | ||
skipLike: undefined, | ||
eNotation: false, | ||
}, | ||
}); | ||
export const parseGetObjectMetaResponse = async (data: string) => { | ||
const res = (await xml.parseStringPromise(data, { | ||
strict: true, | ||
explicitRoot: true, | ||
explicitArray: false, | ||
})) as GetObjectMetaResponse; | ||
|
||
return xmlParser.parse(data) as GetObjectMetaResponse; | ||
const Object = res.GfSpGetObjectMetaResponse.Object || {}; | ||
if (Object) { | ||
Object.Removed = Boolean(Object.Removed); | ||
Object.UpdateAt = Number(Object.UpdateAt); | ||
Object.DeleteAt = Number(Object.DeleteAt); | ||
|
||
Object.ObjectInfo = formatObjectInfo(Object.ObjectInfo); | ||
} | ||
|
||
res.GfSpGetObjectMetaResponse = { | ||
...res.GfSpGetObjectMetaResponse, | ||
Object, | ||
}; | ||
|
||
return res; | ||
}; |
48 changes: 24 additions & 24 deletions
48
packages/chain-sdk/src/clients/spclient/spApis/getUserBuckets.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,34 @@ | ||
import { GetUserBucketsResponse } from '@/types'; | ||
import { XMLParser } from 'fast-xml-parser'; | ||
import xml from 'xml2js'; | ||
|
||
// https://docs.bnbchain.org/greenfield-docs/docs/api/storgae-provider-rest/get_user_buckets | ||
export const parseGetUserBucketsResponse = (data: string) => { | ||
const xmlParser = new XMLParser({ | ||
isArray: (tagName: string) => { | ||
if (tagName === 'Buckets') return true; | ||
return false; | ||
}, | ||
numberParseOptions: { | ||
hex: false, | ||
leadingZeros: true, | ||
skipLike: undefined, | ||
eNotation: false, | ||
}, | ||
}); | ||
export const parseGetUserBucketsResponse = async (data: string) => { | ||
const res = (await xml.parseStringPromise(data, { | ||
strict: true, | ||
explicitRoot: true, | ||
explicitArray: false, | ||
})) as GetUserBucketsResponse; | ||
|
||
const res = xmlParser.parse(data) as GetUserBucketsResponse; | ||
let Buckets = res.GfSpGetUserBucketsResponse.Buckets || []; | ||
if (Buckets) { | ||
if (!Array.isArray(Buckets)) { | ||
Buckets = [Buckets]; | ||
} | ||
|
||
if (typeof res.GfSpGetUserBucketsResponse === 'string') { | ||
return { | ||
GfSpGetUserBucketsResponse: { | ||
Buckets: [], | ||
}, | ||
}; | ||
Buckets = Buckets.map((item) => { | ||
return { | ||
...item, | ||
Removed: Boolean(item.Removed), | ||
DeleteAt: Number(item.DeleteAt), | ||
UpdateAt: Number(item.UpdateAt), | ||
UpdateTime: Number(item.UpdateTime), | ||
}; | ||
}); | ||
} | ||
|
||
if (!res.GfSpGetUserBucketsResponse?.Buckets) { | ||
res.GfSpGetUserBucketsResponse.Buckets = []; | ||
} | ||
res.GfSpGetUserBucketsResponse = { | ||
Buckets, | ||
}; | ||
|
||
return res; | ||
}; |
55 changes: 33 additions & 22 deletions
55
packages/chain-sdk/src/clients/spclient/spApis/listObjectsByBucket.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,42 @@ | ||
import { ListObjectsByBucketNameResponse } from '@/types/sp-xml/ListObjectsByBucketNameResponse'; | ||
import { XMLParser } from 'fast-xml-parser'; | ||
import { | ||
formatObjectInfo, | ||
ListObjectsByBucketNameResponse, | ||
} from '@/types/sp-xml/ListObjectsByBucketNameResponse'; | ||
import xml from 'xml2js'; | ||
|
||
// https://docs.bnbchain.org/greenfield-docs/docs/api/storgae-provider-rest/list_objects_by_bucket | ||
export const parseListObjectsByBucketNameResponse = (data: string) => { | ||
const arrayFields = ['Objects', 'CommonPrefixes']; | ||
const xmlParser = new XMLParser({ | ||
isArray: (tagName: string) => { | ||
if (arrayFields.includes(tagName)) return true; | ||
return false; | ||
}, | ||
numberParseOptions: { | ||
hex: false, | ||
leadingZeros: true, | ||
skipLike: undefined, | ||
eNotation: false, | ||
}, | ||
}); | ||
export const parseListObjectsByBucketNameResponse = async (data: string) => { | ||
const res = (await xml.parseStringPromise(data, { | ||
strict: true, | ||
explicitRoot: true, | ||
explicitArray: false, | ||
})) as ListObjectsByBucketNameResponse; | ||
|
||
const res = xmlParser.parse(data) as ListObjectsByBucketNameResponse; | ||
let Objects = res.GfSpListObjectsByBucketNameResponse.Objects || []; | ||
if (Objects) { | ||
if (!Array.isArray(Objects)) { | ||
Objects = [Objects]; | ||
} | ||
|
||
if (!res.GfSpListObjectsByBucketNameResponse?.CommonPrefixes) { | ||
res.GfSpListObjectsByBucketNameResponse.CommonPrefixes = []; | ||
Objects = Objects.map((item) => { | ||
return { | ||
...item, | ||
Removed: Boolean(item.Removed), | ||
UpdateAt: Number(item.UpdateAt), | ||
DeleteAt: Number(item.DeleteAt), | ||
ObjectInfo: formatObjectInfo(item.ObjectInfo), | ||
}; | ||
}); | ||
} | ||
|
||
if (!res.GfSpListObjectsByBucketNameResponse?.Objects) { | ||
res.GfSpListObjectsByBucketNameResponse.Objects = []; | ||
} | ||
const CommonPrefixes = res.GfSpListObjectsByBucketNameResponse.CommonPrefixes || []; | ||
|
||
res.GfSpListObjectsByBucketNameResponse = { | ||
...res.GfSpListObjectsByBucketNameResponse, | ||
Objects, | ||
CommonPrefixes, | ||
IsTruncated: Boolean(res.GfSpListObjectsByBucketNameResponse.IsTruncated), | ||
}; | ||
|
||
return res; | ||
}; |
Oops, something went wrong.