PDSClient

Updated at:

This topic describes how to use the PDSClient class. It covers the constructor, public methods, and event listeners.

PDSClient constructor

Creates an instance that can call all PDS API operations.

const client = new PDSClient(config, context);

Parameters

Field

Type

Required

Description

config

IClientParams

Yes

For more information, see the description of IClientParams fields below.

context

IContext

No

The execution environment context. For more information, see the description of IContext.

IClientParams fields

Field

Type

Required

Description

token_info

TokenInfo

No

The token information issued through OAuth logon or by a JSON Web Token (JWT) App.

api_endpoint

string

Yes

The PDS API address. The format is usually https://{domainId}.api.aliyunpds.com.

auth_endpoint

string

Yes

The PDS Auth address. The format is usually https://{domainId}.api.aliyunpds.com.

path_type

string

No

The storage path type of the domain. Valid values: StandardMode and HostingMode. Default value: StandardMode.

version

string

No

The version number. Only v2 is supported. Default value: v2.

refresh_token_fun

Function

No

The callback function invoked when the token expires. This method must return a Promise<TokenInfo>.

  • Specify at least one of api_endpoint and auth_endpoint.

TokenInfo fields

Field

Type

Required

Description

access_token

string

Yes

The JWT that represents the user identity. It is typically valid for 2 hours.

refresh_token

string

No

The refresh token. It can be used to refresh an expired access_token.

expire_time

string

No

The expiration time of the access_token. This is typically 2 hours after the token is issued.

expires_in

string

No

The time-to-live (TTL) of the access_token in seconds. For example: 7200.

token_type

string

No

The access_token type. Default value: Bearer.

user_id

string

No

The unique ID of the current user.

role

string

No

The role of the current user.

...Other fields

For information about other fields, see Token - Obtain an access token.

IContext fields

Important

You do not need to pass IContext in a web browser or Node.js environment.

However, in an Electron rendering process, the SDK identifies the environment as a browser. You must pass the context of the Node.js environment. In the Node.js environment (the Electron main process), export the context from the SDK to a preload script that the rendering process can call.

Field

Type

Required

Description

isNode

boolean

Yes

Specifies whether the environment is Node.js.

Axios

object

Yes

For more information, see the axios module.

AxiosNodeAdapter

object

No

Export the AxiosNodeAdapter from the Context in the JS SDK in a Node.js environment.

platform

string

No

This is process.platform in Node.js.

Valid values: "win32", "darwin", and "linux".

os

object

No

The native os module of Node.js.

fs

object

No

The native fs module of Node.js.

path

object

No

The native path module of Node.js.

cp

object

No

The native child_process module of Node.js.

http

object

No

The native http module of Node.js.

https

object

No

The native https module of Node.js.

crypto

object

No

The native crypto module of Node.js.

worker_threads

object

No

The native worker_threads module of Node.js.

How to obtain a token

You can obtain a token in several ways depending on the application type. For more information, see Application overview.

How to refresh a token

An access_token is valid for 2 hours. A refresh_token is valid for 7 days. You can refresh the token at any time before the refresh_token expires, regardless of whether the access_token has expired.

After you refresh the token, the old access_token and refresh_token become invalid.

Steps:

  • When you initialize PDSClient, configure refresh_token_fun.

const client = new PDSClient({
   refresh_token_fun: async ()=>{
     // Call the backend to refresh the token. my_refresh_token_fun is your custom function to refresh the token.
     const token_info = await my_refresh_token_fun()
     return token_info
   }
});

Basic public methods

setToken method

If you do not pass token_info to the constructor, call this method to provide it.

Ensure the client has a valid token_info before you call backend API operations.

client.setToken(token_info)

Parameters

Field

Type

Required

Description

token_info

TokenInfo

Yes

The token_info to pass.

Returns

None

postAPI method

A basic method for service invocation. Use this method to encapsulate calls to any PDS API operation.

API reference: CreateFile

const result = await client.postAPI(pathname, data={}, options={})

Parameters

Field

Type

Required

Description

pathname

string

Yes

The pathname of the API service to call.

For standard mode, if the API operation is /v2/file/list, pass /file/list.

For hosting mode, if the API operation is /v2/hosting/file/list, also pass /file/list.

data

object

No

The JSON parameters for the API operation that corresponds to the pathname.

options

AxiosRequestConfig

No

Other parameters. This method is encapsulated based on axios. The options parameter corresponds to the request config of Axios.

Returns

Field

Type

Description

result

object

The result data returned by the API operation.

Example

let {items =[]} = await client.postAPI('/file/list', {
  drive_id: '1050', 
  parent_file_id: 'root'
})
console.log(items)

postAuth method

A basic method for invoking Auth Endpoint operations. Use this method to encapsulate calls to any PDS Auth Endpoint operation.

API reference: GetLinkInfoByUserId

const result = await client.postAuth(pathname, data={}, options={})

Parameters

Field

Type

Required

Description

pathname

string

Yes

The pathname of the Auth service to call.

For example, if the API operation is /v2/account/cancel_link, pass /account/cancel_link.

data

object

No

The JSON parameters for the API operation that corresponds to the pathname.

options

object

No

Other parameters. This method is encapsulated based on axios. The options parameter corresponds to the request config of Axios.

Returns

Field

Type

Description

result

object

The result data returned by the API operation.

Event listeners

Currently, only the error event is supported.

error event

client.on('error', (error, req_opt)=> {
   console.log('Error',  error)
   console.log('Request Options:', req_opt)
})

Callback function parameters

Field

Type

Description

error

PDSError

The error message. The type is PDSError, which typically includes the {code,message,reqId,status} fields.

req_opt

AxiosRequestConfig

The request options. This method is encapsulated based on axios. The options parameter corresponds to the request config of Axios.

PDSError fields

Field

Type

Description

status

number

The http status returned by the server-side when an error occurs. This field is empty if a client-side error occurs.

code

string

The code field returned by the server-side when an error occurs. If a client-side error occurs, this field is usually ClientError.

message

string

The message field returned by the server-side when an error occurs.

reqId

string

The requestId field returned by the server-side when an error occurs. This field is empty if a client-side error occurs.

stack

string

The error stack for debugging.