-
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.
- Loading branch information
Showing
8 changed files
with
328 additions
and
262 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 |
---|---|---|
@@ -1,103 +1,113 @@ | ||
import * as rm from 'typed-rest-client/RestClient.js' | ||
|
||
import Config from './config.js' | ||
|
||
|
||
export namespace KeClient { | ||
export type ObjectResponse = { | ||
id: number | ||
abspath: string | ||
owner: string | ||
display_name: string | ||
description: string | ||
type_object: string | ||
parent_object: string | ||
created: string | ||
updated: string | ||
fields: { | ||
[key: string]: any | ||
} | ||
extra_properties: { | ||
[key: string]: any | ||
} | ||
user_permissions: { | ||
[key: string]: any | ||
} | ||
group_permissions: { | ||
[key: string]: any | ||
} | ||
export type ObjectResponse = { | ||
abspath: string | ||
created: string | ||
description: string | ||
display_name: string | ||
extra_properties: { | ||
[key: string]: object | ||
} | ||
export type ObjectsResponse = { | ||
count: number, | ||
results: [ObjectResponse] | ||
fields: { | ||
[key: string]: object | ||
} | ||
group_permissions: { | ||
[key: string]: object | ||
} | ||
id: number | ||
owner: string | ||
parent_object: string | ||
type_object: string | ||
updated: string | ||
user_permissions: { | ||
[key: string]: object | ||
} | ||
} | ||
|
||
function request_options(token?: string): rm.IRequestOptions { | ||
return { | ||
'additionalHeaders': { | ||
'Authorization': `Token ${token}` | ||
} | ||
export type ObjectsResponse = { | ||
count: number, | ||
results: [ObjectResponse] | ||
} | ||
|
||
function requestOpts(token?: string): rm.IRequestOptions { | ||
return { | ||
'additionalHeaders': { | ||
'Authorization': `Token ${token}` | ||
} | ||
} | ||
} | ||
|
||
export async function get(config: Config, path: string): Promise<ObjectResponse | null> { | ||
let rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl) | ||
let resp: rm.IRestResponse<ObjectResponse> = await rest.get<ObjectResponse>(path, request_options(config.token)) | ||
if (resp.statusCode == 404) { | ||
return null | ||
} else if (resp.statusCode != 200) { | ||
throw new Error(`StatusCode: ${resp.statusCode}`) | ||
} | ||
return resp.result | ||
export async function get(config: Config, path: string): Promise<ObjectResponse | null> { | ||
const rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl) | ||
const resp: rm.IRestResponse<ObjectResponse> = await rest.get<ObjectResponse>(path, requestOpts(config.token)) | ||
if (resp.statusCode === 404) { | ||
return null | ||
} | ||
|
||
export async function get_all(config: Config, path: string): Promise<ObjectsResponse | null> { | ||
let rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl) | ||
let resp: rm.IRestResponse<ObjectsResponse> = await rest.get<ObjectsResponse>(path, request_options(config.token)) | ||
if (resp.statusCode == 404) { | ||
return null | ||
} else if (resp.statusCode != 200) { | ||
throw new Error(`StatusCode: ${resp.statusCode}`) | ||
} | ||
return resp.result | ||
if (resp.statusCode !== 200) { | ||
throw new Error(`StatusCode: ${resp.statusCode}`) | ||
} | ||
|
||
export async function create(config: Config, path: string, data: any): Promise<ObjectResponse> { | ||
let rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl) | ||
let resp: rm.IRestResponse<ObjectResponse> = await rest.create<ObjectResponse>(path, data, request_options(config.token)) | ||
if (resp.statusCode != 201) { | ||
throw new Error(`StatusCode: ${resp.statusCode}`) | ||
} | ||
return resp.result as ObjectResponse | ||
return resp.result | ||
} | ||
|
||
export async function getAll(config: Config, path: string): Promise<ObjectsResponse | null> { | ||
const rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl) | ||
const resp: rm.IRestResponse<ObjectsResponse> = await rest.get<ObjectsResponse>(path, requestOpts(config.token)) | ||
if (resp.statusCode === 404) { | ||
return null | ||
} | ||
|
||
export async function update(config: Config, path: string, data: any): Promise<ObjectResponse> { | ||
let rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl) | ||
let resp: rm.IRestResponse<ObjectResponse> = await rest.update<ObjectResponse>(path, data, request_options(config.token)) | ||
if (resp.statusCode != 200) { | ||
throw new Error(`StatusCode: ${resp.statusCode}`) | ||
} | ||
return resp.result as ObjectResponse | ||
if (resp.statusCode !== 200) { | ||
throw new Error(`StatusCode: ${resp.statusCode}`) | ||
} | ||
|
||
export async function replace(config: Config, path: string, data: any): Promise<ObjectResponse> { | ||
let rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl) | ||
let resp: rm.IRestResponse<ObjectResponse> = await rest.replace<ObjectResponse>(path, data, request_options(config.token)) | ||
if (resp.statusCode != 200) { | ||
throw new Error(`StatusCode: ${resp.statusCode}`) | ||
} | ||
return resp.result as ObjectResponse | ||
return resp.result | ||
} | ||
|
||
export async function create(config: Config, path: string, data: object): Promise<ObjectResponse> { | ||
const rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl) | ||
const resp: rm.IRestResponse<ObjectResponse> = await rest.create<ObjectResponse>(path, data, requestOpts(config.token)) | ||
if (resp.statusCode !== 201) { | ||
throw new Error(`StatusCode: ${resp.statusCode}`) | ||
} | ||
|
||
export async function del(config: Config, path: string, force: boolean = false): Promise<boolean> { | ||
let rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl) | ||
let resp: rm.IRestResponse<ObjectResponse> = await rest.del<ObjectResponse>(path, request_options(config.token)) | ||
if (force && resp.statusCode == 404) { | ||
return false | ||
} else if (resp.statusCode != 204) { | ||
throw new Error(`StatusCode: ${resp.statusCode}`) | ||
} | ||
return true | ||
return resp.result as ObjectResponse | ||
} | ||
|
||
export async function update(config: Config, path: string, data: object): Promise<ObjectResponse> { | ||
const rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl) | ||
const resp: rm.IRestResponse<ObjectResponse> = await rest.update<ObjectResponse>(path, data, requestOpts(config.token)) | ||
if (resp.statusCode !== 200) { | ||
throw new Error(`StatusCode: ${resp.statusCode}`) | ||
} | ||
|
||
return resp.result as ObjectResponse | ||
} | ||
|
||
export async function replace(config: Config, path: string, data: object): Promise<ObjectResponse> { | ||
const rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl) | ||
const resp: rm.IRestResponse<ObjectResponse> = await rest.replace<ObjectResponse>(path, data, requestOpts(config.token)) | ||
if (resp.statusCode !== 200) { | ||
throw new Error(`StatusCode: ${resp.statusCode}`) | ||
} | ||
|
||
return resp.result as ObjectResponse | ||
} | ||
|
||
export default KeClient | ||
export async function del(config: Config, path: string, force: boolean = false): Promise<boolean> { | ||
const rest: rm.RestClient = new rm.RestClient('ke-client', config.baseurl) | ||
const resp: rm.IRestResponse<ObjectResponse> = await rest.del<ObjectResponse>(path, requestOpts(config.token)) | ||
if (force && resp.statusCode === 404) { | ||
return false | ||
} | ||
|
||
if (resp.statusCode !== 204) { | ||
throw new Error(`StatusCode: ${resp.statusCode}`) | ||
} | ||
|
||
return true | ||
} |
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
Oops, something went wrong.