forked from goplus/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apis/user): complete
User
with displayName
and avatar
form…
… Casdoor Signed-off-by: Aofei Sheng <aofei@aofeisheng.com>
- Loading branch information
Showing
7 changed files
with
174 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { casdoorClient } from './common' | ||
import { casdoorConfig } from '@/utils/env' | ||
|
||
export type CasdoorUser = { | ||
id: string | ||
name: string | ||
displayName: string | ||
avatar: string | ||
} | ||
|
||
export async function getCasdoorUser(name: string): Promise<CasdoorUser> { | ||
return casdoorClient.get('/api/get-user', { | ||
id: `${casdoorConfig.organizationName}/${name}` | ||
}) as Promise<CasdoorUser> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { casdoorConfig } from '@/utils/env' | ||
import { CasdoorApiException } from './exception' | ||
import { useRequest, withQueryParams, type RequestOptions, type QueryParams } from '.' | ||
|
||
/** Response body when exception encountered for Casdoor API calling */ | ||
export type CasdoorApiExceptionPayload = { | ||
/** Message for developer reading */ | ||
msg: string | ||
} | ||
|
||
function isCasdoorApiExceptionPayload(body: any): body is CasdoorApiExceptionPayload { | ||
return body && typeof body.msg === 'string' | ||
} | ||
|
||
export class CasdoorClient { | ||
private request = useRequest(casdoorConfig.serverUrl, async (resp) => { | ||
if (!resp.ok) { | ||
const body = await resp.json() | ||
if (!isCasdoorApiExceptionPayload(body)) { | ||
throw new Error('casdoor api call failed') | ||
} | ||
throw new CasdoorApiException(body.msg) | ||
} | ||
if (resp.status === 204) return null | ||
const body = await resp.json() | ||
return body.data | ||
}) | ||
|
||
get(path: string, params?: QueryParams, options?: Omit<RequestOptions, 'method'>) { | ||
if (params != null) path = withQueryParams(path, params) | ||
return this.request(path, null, { ...options, method: 'GET' }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters