mirror of
https://github.com/LukeHagar/plexjs.git
synced 2025-12-07 12:37:45 +00:00
ci: regenerated with OpenAPI Doc 0.0.3, Speakeasy CLI 1.193.0
This commit is contained in:
85
src/hooks/hooks.ts
Normal file
85
src/hooks/hooks.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
|
||||
*/
|
||||
|
||||
import {
|
||||
AfterErrorContext,
|
||||
AfterErrorHook,
|
||||
AfterSuccessContext,
|
||||
AfterSuccessHook,
|
||||
BeforeRequestContext,
|
||||
BeforeRequestHook,
|
||||
ClientInitHook,
|
||||
Hooks,
|
||||
} from "./types";
|
||||
|
||||
import { HTTPClient } from "../lib/http";
|
||||
import { initHooks } from "./registration";
|
||||
|
||||
export class SDKHooks implements Hooks {
|
||||
clientInitHooks: ClientInitHook[] = [];
|
||||
beforeRequestHooks: BeforeRequestHook[] = [];
|
||||
afterSuccessHooks: AfterSuccessHook[] = [];
|
||||
afterErrorHooks: AfterErrorHook[] = [];
|
||||
|
||||
constructor() {
|
||||
initHooks(this);
|
||||
}
|
||||
|
||||
registerClientInitHook(hook: ClientInitHook) {
|
||||
this.clientInitHooks.push(hook);
|
||||
}
|
||||
|
||||
registerBeforeRequestHook(hook: BeforeRequestHook) {
|
||||
this.beforeRequestHooks.push(hook);
|
||||
}
|
||||
|
||||
registerAfterSuccessHook(hook: AfterSuccessHook) {
|
||||
this.afterSuccessHooks.push(hook);
|
||||
}
|
||||
|
||||
registerAfterErrorHook(hook: AfterErrorHook) {
|
||||
this.afterErrorHooks.push(hook);
|
||||
}
|
||||
|
||||
clientInit(client: HTTPClient): HTTPClient {
|
||||
return this.clientInitHooks.reduce((client, hook) => hook.clientInit(client), client);
|
||||
}
|
||||
|
||||
async beforeRequest(hookCtx: BeforeRequestContext, request: Request): Promise<Request> {
|
||||
let req = request;
|
||||
|
||||
for (const hook of this.beforeRequestHooks) {
|
||||
req = await hook.beforeRequest(hookCtx, req);
|
||||
}
|
||||
|
||||
return req;
|
||||
}
|
||||
|
||||
async afterSuccess(hookCtx: AfterSuccessContext, response: Response): Promise<Response> {
|
||||
let res = response;
|
||||
|
||||
for (const hook of this.afterSuccessHooks) {
|
||||
res = await hook.afterSuccess(hookCtx, res);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
async afterError(
|
||||
hookCtx: AfterErrorContext,
|
||||
response: Response | null,
|
||||
error: unknown
|
||||
): Promise<{ response: Response | null; error: unknown }> {
|
||||
let res = response;
|
||||
let err = error;
|
||||
|
||||
for (const hook of this.afterErrorHooks) {
|
||||
const result = await hook.afterError(hookCtx, res, err);
|
||||
res = result.response;
|
||||
err = result.error;
|
||||
}
|
||||
|
||||
return { response: res, error: err };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user