PDSClient
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 |
api_endpoint | string | Yes | The PDS API address. The format is usually |
auth_endpoint | string | Yes | The PDS Auth address. The format is usually |
path_type | string | No | The storage path type of the domain. Valid values: |
version | string | No | The version number. Only |
refresh_token_fun | Function | No | The callback function invoked when the |
Specify at least one of
api_endpointandauth_endpoint.
TokenInfo fields
Field | Type | Required | Description |
access_token | string | Yes | The |
refresh_token | string | No | The refresh |
expire_time | string | No | The expiration time of the |
expires_in | string | No | The time-to-live (TTL) of the |
token_type | string | No | The |
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
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 |
platform | string | No | This is Valid values: " |
os | object | No | The native |
fs | object | No | The native |
path | object | No | The native |
cp | object | No | The native |
http | object | No | The native |
https | object | No | The native |
crypto | object | No | The native |
worker_threads | object | No | The native |
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, configurerefresh_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
}
});Encapsulate the
tokenrefresh method. For more information, see Token - Obtain an access token.
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 For hosting mode, if the API operation is |
data | object | No | The JSON parameters for the API operation that corresponds to the |
options | AxiosRequestConfig | No | Other parameters. This method is encapsulated based on axios. The |
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 |
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 |
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 |
code | string | The |
message | string | The |
reqId | string | The |
stack | string | The error stack for debugging. |