Skip to content

Commit

Permalink
fix: fix axios error request call (#35)
Browse files Browse the repository at this point in the history
* fix: fix axios error request call

* fix: replace btoa function with Buffer.from
  • Loading branch information
huyonger authored Oct 31, 2023
1 parent 55e00ff commit 5038ea2
Show file tree
Hide file tree
Showing 5 changed files with 1,006 additions and 1,079 deletions.
68 changes: 39 additions & 29 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ export interface Application {
homepageUrl: string
description: string
organization: string
cert: string
enablePassword: boolean
enableSignUp: boolean
enableSigninSession: boolean
enableAutoSignin: boolean
enableCodeSignin: boolean
enableSamlCompress: boolean
enableWebAuthn: boolean
enableLinkWithEmail: boolean
cert?: string
enablePassword?: boolean
enableSignUp?: boolean
enableSigninSession?: boolean
enableCodeSignin?: boolean
enableAutoSignin?: boolean
enableSamlCompress?: boolean
enableWebAuthn?: boolean
enableLinkWithEmail?: boolean
orgChoiceMode?: string
samlReplyUrl?: string
providers?: ProviderItem[]
Expand Down Expand Up @@ -99,24 +99,34 @@ export class ApplicationSDK {
return (await this.request.get('/get-applications', {
params: {
owner: 'admin',
clientId: this.config.clientId,
clientSecret: this.config.clientSecret,
},
})) as unknown as Promise<AxiosResponse<Application[]>>
headers: {
Authorization:
'Basic ' +
Buffer.from(
`${this.config.clientId}:${this.config.clientSecret}`,
).toString('base64'),
},
})) as unknown as Promise<AxiosResponse<{ data: Application[] }>>
}

public async getApplication(id: string) {
public async getApplication(name: string) {
if (!this.request) {
throw new Error('request init failed')
}

return (await this.request.get('/get-application', {
params: {
id: `${this.config.orgName}/${id}`,
clientId: this.config.clientId,
clientSecret: this.config.clientSecret,
id: `admin/${name}`,
},
headers: {
Authorization:
'Basic ' +
Buffer.from(
`${this.config.clientId}:${this.config.clientSecret}`,
).toString('base64'),
},
})) as unknown as Promise<AxiosResponse<Application>>
})) as unknown as Promise<AxiosResponse<{ data: Application }>>
}

public async modifyApplication(method: string, application: Application) {
Expand All @@ -125,19 +135,19 @@ export class ApplicationSDK {
}

const url = `/${method}`
application.owner = this.config.orgName
const applicationInfo = JSON.stringify(application)
return (await this.request.post(
url,
{ applicationInfo },
{
params: {
id: `${application.owner}/${application.name}`,
clientId: this.config.clientId,
clientSecret: this.config.clientSecret,
},
application.owner = 'admin'
return (await this.request.post(url, application, {
params: {
id: `${application.owner}/${application.name}`,
},
headers: {
Authorization:
'Basic ' +
Buffer.from(
`${this.config.clientId}:${this.config.clientSecret}`,
).toString('base64'),
},
)) as unknown as Promise<AxiosResponse<Record<string, unknown>>>
})) as unknown as Promise<AxiosResponse<Record<string, unknown>>>
}

public async addApplication(application: Application) {
Expand Down
6 changes: 1 addition & 5 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ export default class Request {
return this.client.get(url, config)
}

post(
url: string,
data?: Record<string, string>,
config?: AxiosRequestConfig<any>,
) {
post(url: string, data: any, config?: AxiosRequestConfig<any>) {
return this.client.post(url, data, config)
}
}
4 changes: 2 additions & 2 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ export class SDK {
return await this.applicationSDK.getApplications()
}

public async getApplication(id: string) {
return await this.applicationSDK.getApplication(id)
public async getApplication(name: string) {
return await this.applicationSDK.getApplication(name)
}

public async addApplication(application: Application) {
Expand Down
34 changes: 23 additions & 11 deletions test/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,24 @@ test('TestApplication', async () => {
organization: 'casbin',
}

const addResponse = await sdk.addApplication(application)
if (!addResponse) {
const { data: addResponse } = await sdk.addApplication(application)
if (addResponse.data !== 'Affected') {
throw new Error('Failed to add object')
}

// Get all objects and check if our added object is in the list
const applications = await sdk.getApplications()
const found = applications.some((item) => item.Name === name)
const {
data: { data: applications },
} = await sdk.getApplications()
const found = applications.some((item) => item.name === name)
if (!found) {
throw new Error('Added object not found in list')
}

// Get the object
const retrievedApplication = await sdk.getApplication(name)
const {
data: { data: retrievedApplication },
} = await sdk.getApplication(name)
if (retrievedApplication.name !== name) {
throw new Error(
`Retrieved object does not match added object: ${retrievedApplication.name} != ${name}`,
Expand All @@ -65,27 +69,35 @@ test('TestApplication', async () => {
// Update the object
const updatedDescription = 'Updated Casdoor Website'
retrievedApplication.description = updatedDescription
const updateResponse = await sdk.updateApplication(retrievedApplication)
if (!updateResponse) {
const { data: updateResponse } = await sdk.updateApplication(
retrievedApplication,
)
if (updateResponse.data !== 'Affected') {
throw new Error('Failed to update object')
}

// Validate the update
const updatedApplication = await sdk.getApplication(name)
const {
data: { data: updatedApplication },
} = await sdk.getApplication(name)
if (updatedApplication.description !== updatedDescription) {
throw new Error(
`Failed to update object, description mismatch: ${updatedApplication.description} != ${updatedDescription}`,
)
}

// Delete the object
const deleteResponse = await sdk.deleteApplication(retrievedApplication)
if (!deleteResponse) {
const { data: deleteResponse } = await sdk.deleteApplication(
retrievedApplication,
)
if (deleteResponse.data !== 'Affected') {
throw new Error('Failed to delete object')
}

// Validate the deletion
const deletedApplication = await sdk.getApplication(name)
const {
data: { data: deletedApplication },
} = await sdk.getApplication(name)
if (deletedApplication) {
throw new Error("Failed to delete object, it's still retrievable")
}
Expand Down
Loading

0 comments on commit 5038ea2

Please sign in to comment.