-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(odc): command-specific configuration
- Loading branch information
Showing
21 changed files
with
309 additions
and
199 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/// <reference types="node" /> | ||
|
||
export interface Executor<Config = {}> { | ||
execute( | ||
request: { | ||
method: string; | ||
path: string; | ||
headers?: null | undefined | Record<string, string>; | ||
params?: null | undefined | Record<string, unknown>; | ||
data?: null | undefined | RequestInit['body']; | ||
}, | ||
config?: Config | ||
): Promise<Response>; | ||
} |
This file was deleted.
Oops, something went wrong.
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,98 @@ | ||
import type { Executor } from './Executor.js'; | ||
import { ODCError } from './ODCError.js'; | ||
|
||
type ODCConfig = { | ||
address: string; | ||
signal?: AbortSignal; | ||
}; | ||
|
||
type ODCExecutorConfig = { | ||
signal?: AbortSignal; | ||
}; | ||
|
||
export class ODCExecutor implements Executor<ODCExecutorConfig> { | ||
#config: ODCConfig; | ||
|
||
constructor(config: ODCConfig) { | ||
this.#config = config; | ||
} | ||
|
||
async execute( | ||
request: { | ||
method: string; | ||
path: string; | ||
headers?: null | undefined | Record<string, string>; | ||
params?: null | undefined | Record<string, unknown>; | ||
data?: null | undefined | RequestInit['body']; | ||
}, | ||
config?: ODCExecutorConfig | ||
): Promise<Response> { | ||
const signal = | ||
this.#config.signal && config?.signal | ||
? // @ts-ignore | ||
AbortSignal.any([this.#config.signal, config.signal]) | ||
: this.#config.signal || config?.signal; | ||
|
||
let path = request.path; | ||
|
||
if (request.params) { | ||
const params = Object.entries(request.params).reduce( | ||
(result, [key, value]) => { | ||
result[key] = ['string', 'number', 'boolean'].includes(typeof value) | ||
? value | ||
: JSON.stringify(value); | ||
return result; | ||
}, | ||
{} as Record<string, any> | ||
); | ||
|
||
path += '?' + new URLSearchParams(params); | ||
} | ||
|
||
const requestInit: RequestInit = { | ||
signal, | ||
method: request.method, | ||
}; | ||
|
||
if (request.headers) { | ||
requestInit.headers = request.headers; | ||
} | ||
|
||
if (request.data) { | ||
requestInit.body = request.data; | ||
} | ||
|
||
const response = await fetch( | ||
this.#config.address + '/' + path, | ||
requestInit | ||
); | ||
|
||
if (!response.ok) { | ||
const json: any = await response.json(); | ||
|
||
let trace = json.backtrace?.reverse() || []; | ||
let message = | ||
json.message || `${request.method} /${path} -> ${response.status}`; | ||
let error = message; | ||
|
||
if (trace.length) { | ||
error += '\n at '; | ||
error += trace | ||
.map( | ||
(trace: any) => | ||
trace.function.replace(/\(.+/, '') + | ||
' (' + | ||
trace.filename + | ||
':' + | ||
trace.line_number + | ||
')' | ||
) | ||
.join('\n at '); | ||
} | ||
|
||
throw new ODCError(error); | ||
} | ||
|
||
return response; | ||
} | ||
} |
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,4 +1,4 @@ | ||
export type ODCOptions = { | ||
address: string; | ||
signal?: AbortSignal; | ||
} | ||
}; |
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,9 @@ | ||
import type { Executor } from '../Executor.js'; | ||
import type { Config } from '../internal/Config.js'; | ||
|
||
export async function clearRegistry<Context extends Executor>( | ||
ctx: Context, | ||
config?: Config<Context> | ||
): Promise<void> { | ||
await ctx.execute({ method: 'DELETE', path: 'registry' }, config); | ||
} |
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,5 @@ | ||
import inject from '../internal/injector.js'; | ||
|
||
export async function extend(app: Buffer): Promise<Buffer> { | ||
return await inject(app); | ||
} |
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 type { Executor } from '../Executor.js'; | ||
import type { Config } from '../internal/Config.js'; | ||
import type { Nullable } from '../internal/Nullable.js'; | ||
|
||
export async function getAppUI<Context extends Executor>( | ||
ctx: Context, | ||
params?: Nullable<{ fields?: Nullable<Record<string, string[]>> }>, | ||
config?: Config<Context> | ||
): Promise<string> { | ||
const response = await ctx.execute( | ||
{ method: 'GET', path: 'app-ui', params }, | ||
config | ||
); | ||
return response.text(); | ||
} |
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,16 @@ | ||
import type { Executor } from '../Executor.js'; | ||
import type { Config } from '../internal/Config.js'; | ||
import type { Directory } from '../types/Directory.js'; | ||
import type { File } from '../types/File.js'; | ||
|
||
export async function getFiles<Context extends Executor>( | ||
ctx: Context, | ||
params: { path: string }, | ||
config?: Config<Context> | ||
): Promise<(File | Directory)[]> { | ||
const response = await ctx.execute( | ||
{ method: 'GET', path: 'files', params }, | ||
config | ||
); | ||
return response.json() as any; | ||
} |
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,13 @@ | ||
import type { Executor } from '../Executor.js'; | ||
import type { Config } from '../internal/Config.js'; | ||
|
||
export async function getRegistry<Context extends Executor>( | ||
ctx: Context, | ||
config?: Config<Context> | ||
): Promise<Record<string, Record<string, string>>> { | ||
const response = await ctx.execute( | ||
{ method: 'GET', path: 'registry' }, | ||
config | ||
); | ||
return response.json() as any; | ||
} |
Oops, something went wrong.