Skip to content

Commit

Permalink
feat: add resource test case (#39)
Browse files Browse the repository at this point in the history
* chore(yarn.lock): update yarn.lock file by nodejs18

* test: add test cases for all objects and all operations

* feat: add resource test case
  • Loading branch information
huyonger authored Nov 4, 2023
1 parent 3ab8905 commit b5541cb
Show file tree
Hide file tree
Showing 7 changed files with 1,098 additions and 946 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
},
"dependencies": {
"axios": "^0.22.0",
"jsonwebtoken": "^8.5.1"
"form-data": "^4.0.0",
"jsonwebtoken": "^8.5.1",
"node-fetch": "^3.3.2"
}
}
10 changes: 10 additions & 0 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'
import * as FormData from 'form-data'

export default class Request {
private client: AxiosInstance
Expand All @@ -31,4 +32,13 @@ export default class Request {
post(url: string, data: any, config?: AxiosRequestConfig<any>) {
return this.client.post(url, data, config)
}

postFile(url: string, postFile: any, config?: AxiosRequestConfig<any>) {
const formData = new FormData()
formData.append('file', postFile)
return this.client.post(url, formData, {
params: config?.params,
headers: formData.getHeaders(),
})
}
}
59 changes: 45 additions & 14 deletions src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@ import Request from './request'
export interface Resource {
owner: string
name: string
createdTime: string

user: string
provider: string
application: string
tag: string
parent: string
fileName: string
fileType: string
fileFormat: string
fileSize: number
url: string
description: string
createdTime?: string

user?: string
provider?: string
application?: string
tag?: string
parent?: string
fileName?: string
fileType?: string
fileFormat?: string
fileSize?: number
url?: string
description?: string
fullFilePath?: string
}

export class ResourceSDK {
Expand All @@ -43,6 +44,24 @@ export class ResourceSDK {
this.request = request
}

public async uploadResource(resource: Resource, psotFile: any) {
if (!this.request) {
throw new Error('request init failed')
}

const url = `/upload-resource`
return (await this.request.postFile(url, psotFile, {
params: {
owner: this.config.orgName,
user: resource.owner,
application: this.config.appName,
tag: resource.name,
parent: resource.parent,
fullFilePath: resource.fullFilePath,
},
})) as unknown as Promise<AxiosResponse<Record<string, unknown>>>
}

public async getResources(
owner: string,
user: string,
Expand Down Expand Up @@ -102,6 +121,18 @@ export class ResourceSDK {
}

public async deleteResource(resource: Resource) {
return this.modifyResource('delete-resource', resource)
if (!this.request) {
throw new Error('request init failed')
}

const url = `/delete-resource`
const post = {
owner: resource.owner,
name: resource.name,
}
return (await this.request.post(
url,
JSON.stringify(post),
)) as unknown as Promise<AxiosResponse<Record<string, unknown>>>
}
}
4 changes: 4 additions & 0 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,10 @@ export class SDK {
return await this.resourceSDK.deleteResource(resource)
}

public async uploadResource(resource: Resource, file: any) {
return await this.resourceSDK.uploadResource(resource, file)
}

public async getRoles() {
return await this.roleSDK.getRoles()
}
Expand Down
1 change: 1 addition & 0 deletions test/public/casbinTest.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit b5541cb

Please sign in to comment.