Skip to content

Commit

Permalink
feat: DelegatedCreateFolder api
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 committed Apr 3, 2024
1 parent 9d4463b commit 83f861d
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/eleven-dragons-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-js-sdk': major
---

feat: Support delegatedCreateFolder
34 changes: 34 additions & 0 deletions examples/nextjs/src/components/object/create/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,40 @@ export const CreateObject = () => {
}}
>
create folder
</button>{' '}
<button
onClick={async () => {
if (!address) return;

const provider = await connector?.getProvider();
const offChainData = await getOffchainAuthKeys(address, provider);
if (!offChainData) {
alert('No offchain, please create offchain pairs first');
return;
}

const res = await client.object.delegateCreateFolder(
{
bucketName: createObjectInfo.bucketName,
objectName: createObjectInfo.objectName,
delegatedOpts: {
visibility: VisibilityType.VISIBILITY_TYPE_PUBLIC_READ,
},
},
{
type: 'EDDSA',
domain: window.location.origin,
seed: offChainData.seedString,
address,
},
);

if (res.code === 0) {
alert('success');
}
}}
>
delegate create folder
</button>
</>
</div>
Expand Down
52 changes: 51 additions & 1 deletion packages/js-sdk/src/api/objects.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getDelegatedCreateFolderMetaInfo } from '@/clients/spclient/spApis/delegatedCreateFolder';
import {
getObjectOffsetInfo,
parseObjectOffsetResponse,
Expand Down Expand Up @@ -91,7 +92,12 @@ import {
import { GetObjectRequest } from '../types/sp/GetObject';
import { GetObjectMetaRequest, GetObjectMetaResponse } from '../types/sp/GetObjectMeta';
import { ListObjectsByBucketNameResponse } from '../types/sp/ListObjectsByBucketName';
import { DelegatedOpts, DelegatedPubObjectRequest, PutObjectRequest } from '../types/sp/PutObject';
import {
DelegatedCreateFolderRequest,
DelegatedOpts,
DelegatedPubObjectRequest,
PutObjectRequest,
} from '../types/sp/PutObject';
import {
checkObjectName,
generateUrlByBucketName,
Expand Down Expand Up @@ -144,6 +150,11 @@ export interface IObject {
msg: Omit<MsgCreateObject, 'payloadSize' | 'contentType' | 'expectChecksums'>,
): Promise<TxResponse>;

delegateCreateFolder(
params: DelegatedCreateFolderRequest,
authType: AuthType,
): Promise<SpResponse<null>>;

putObjectPolicy(
bucketName: string,
objectName: string,
Expand Down Expand Up @@ -884,6 +895,45 @@ export class Objects implements IObject {
return this.createObject(newMsg);
}

public async delegateCreateFolder(params: DelegatedCreateFolderRequest, authType: AuthType) {
const { bucketName, objectName, delegatedOpts, timeout = 10000 } = params;

let endpoint = params.endpoint;
if (!endpoint) {
endpoint = await this.sp.getSPUrlByBucket(bucketName);
}
const { reqMeta, optionsWithOutHeaders, url } = await getDelegatedCreateFolderMetaInfo(
endpoint,
{
bucketName: bucketName,
objectName: objectName,
// contentType: '',
delegatedOpts,
},
);
const signHeaders = await this.spClient.signHeaders(reqMeta, authType);

try {
const result = await this.spClient.callApi(
url,
{
...optionsWithOutHeaders,
headers: signHeaders,
},
timeout,
);
const { status } = result;

return { code: 0, message: 'Put object success.', statusCode: status };
} catch (error: any) {
return {
code: -1,
message: error.message,
statusCode: error?.statusCode || NORMAL_ERROR_CODE,
};
}
}

public async putObjectPolicy(
bucketName: string,
objectName: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { EMPTY_STRING_SHA256, METHOD_POST } from '@/constants';
import { ReqMeta } from '@/types';
import { generateUrlByBucketName } from '@/utils';
import { VisibilityType } from '@bnb-chain/greenfield-cosmos-types/greenfield/storage/common';
import { encodePath, getSortQueryParams } from '../auth';

export const getDelegatedCreateFolderMetaInfo = async (
endpoint: string,
params: {
objectName: string;
bucketName: string;
delegatedOpts?: {
visibility: VisibilityType;
};
},
) => {
const { bucketName, objectName, delegatedOpts } = params;
const path = `/${encodePath(objectName)}`;
let queryMap = {};

if (delegatedOpts) {
queryMap = {
'create-folder': '',
payload_size: '0',
visibility: delegatedOpts.visibility.toString(),
};
}

let url = new URL(path, generateUrlByBucketName(endpoint, bucketName));
url = getSortQueryParams(url, queryMap);

const reqMeta: Partial<ReqMeta> = {
contentSHA256: EMPTY_STRING_SHA256,
method: METHOD_POST,
url: {
hostname: url.hostname,
query: url.searchParams.toString(),
path,
},
contentType: '',
};

const optionsWithOutHeaders: Omit<RequestInit, 'headers'> = {
method: METHOD_POST,
};

return {
url: url.href,
optionsWithOutHeaders,
reqMeta,
};
};
8 changes: 8 additions & 0 deletions packages/js-sdk/src/types/sp/PutObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ export type DelegatedPubObjectRequest = {
resumableOpts?: ResumableOpts;
};

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

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

0 comments on commit 83f861d

Please sign in to comment.