-
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
1 parent
c0679de
commit f05fc01
Showing
7 changed files
with
95 additions
and
64 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,3 +1,4 @@ | ||
// deno-lint-ignore no-external-import | ||
import openapi from 'npm:[email protected]'; | ||
|
||
const schema_url = 'https://api.revolt.chat/openapi.json'; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,7 @@ | ||
import { queryParams, type routes } from '@jersey/revolt-api-types'; | ||
import { path_name } from './path_name.ts'; | ||
|
||
/** | ||
* create a request function | ||
* @param base_url the base url to use | ||
* @param headers the headers to use | ||
* @returns a request function | ||
* Delta is the Revolt API requestor | ||
* @module | ||
*/ | ||
export function createRequest( | ||
base_url: string, | ||
headers: Record<string, string>, | ||
): request { | ||
return async (method, path, params) => { | ||
const query = new URLSearchParams(); | ||
const body = {} as Record<string, unknown>; | ||
const named = path_name(path); | ||
|
||
if (named && params && typeof params === 'object') { | ||
const route = queryParams[named as keyof typeof queryParams]; | ||
const allowed_query = (route as Record<typeof method, string[]>)[method]; | ||
for (const param of Object.keys(params)) { | ||
if (allowed_query.includes(param)) { | ||
query.append(param, (params as Record<string, string>)[param]); | ||
} else { | ||
body[param] = (params as Record<string, string>)[param]; | ||
} | ||
} | ||
} | ||
|
||
const req_body = ['head', 'get'].includes(method) | ||
? undefined | ||
: JSON.stringify(body); | ||
|
||
const res = await fetch(`${base_url}${path}?${query.toString()}`, { | ||
method, | ||
headers, | ||
body: req_body, | ||
}); | ||
|
||
if (res.ok) { | ||
return await res.json(); | ||
} else { | ||
throw new Error(`Request failed with status ${res.status}`, { | ||
cause: res, | ||
}); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* make a request against the revolt api | ||
*/ | ||
export type request = < | ||
method extends routes['method'], | ||
path extends (routes & { method: method })['path'], | ||
route extends (routes & { method: method; path: path }), | ||
>( | ||
method: method, | ||
path: path, | ||
params: route['params'], | ||
) => Promise<Exclude<route['response'], string>>; | ||
export * from './request.ts'; | ||
export * from './types.ts'; |
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,52 @@ | ||
import { queryParams } from '@jersey/revolt-api-types'; | ||
import { path_name } from './path_name.ts'; | ||
import { type request, RequestError } from './types.ts'; | ||
|
||
/** | ||
* create a request function | ||
* @param base_url the base url to use | ||
* @param headers the headers to use | ||
* @returns a request function | ||
*/ | ||
export function createRequest( | ||
base_url: string, | ||
headers: Record<string, string>, | ||
): request { | ||
return async (method, path, params) => { | ||
const query = new URLSearchParams(); | ||
const body = {} as Record<string, unknown>; | ||
const named = path_name(path); | ||
|
||
if (named && params && typeof params === 'object') { | ||
const route = queryParams[named as keyof typeof queryParams]; | ||
const allowed_query = (route as Record<typeof method, string[]>)[method]; | ||
for (const param of Object.keys(params)) { | ||
if (allowed_query.includes(param)) { | ||
query.append(param, (params as Record<string, string>)[param]); | ||
} else { | ||
body[param] = (params as Record<string, string>)[param]; | ||
} | ||
} | ||
} | ||
|
||
const req_body = ['head', 'get', 'HEAD', 'GET'].includes(method) | ||
? undefined | ||
: JSON.stringify(body); | ||
|
||
const res = await fetch(`${base_url}${path}?${query.toString()}`, { | ||
method, | ||
headers, | ||
body: req_body, | ||
}); | ||
|
||
if (res.ok) { | ||
try { | ||
return await res.json(); | ||
} catch (e) { | ||
throw e; | ||
} | ||
} else { | ||
throw new RequestError(method, path, params, res); | ||
} | ||
}; | ||
} |
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 type { routes } from '@jersey/revolt-api-types'; | ||
|
||
/** | ||
* make a request against the revolt api | ||
*/ | ||
export type request = < | ||
method extends routes['method'], | ||
path extends (routes & { method: method })['path'], | ||
route extends (routes & { method: method; path: path }), | ||
>( | ||
method: method, | ||
path: path, | ||
params: route['params'], | ||
) => Promise<Exclude<route['response'], string>>; | ||
|
||
/** an error with a request to the api */ | ||
export class RequestError extends Error { | ||
/** the cause of the error from the api */ | ||
override cause: Response; | ||
method: string; | ||
path: string; | ||
params: unknown; | ||
|
||
/** create an error */ | ||
constructor(method: string, path: string, params: unknown, cause: Response) { | ||
super(`Failed to ${method} ${path}: ${cause.status}`); | ||
this.name = 'RequestError'; | ||
this.cause = cause; | ||
this.method = method; | ||
this.path = path; | ||
this.params = params; | ||
} | ||
} |
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