Skip to content

Commit

Permalink
fix after success hook
Browse files Browse the repository at this point in the history
  • Loading branch information
tatarco committed Dec 9, 2024
1 parent f1b2b57 commit e9af132
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 88 deletions.
8 changes: 4 additions & 4 deletions .speakeasy/gen.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ id: 6bff3a53-3f0b-40b4-942f-ff70dc583ae5
management:
docChecksum: e0965dcae4636ea046bdbf2de617abc0
docVersion: "1.0"
speakeasyVersion: 1.453.0
generationVersion: 2.472.1
releaseVersion: 0.0.1-alpha.65
configChecksum: 1274ae1f860be40abc121fd1767a1cfb
speakeasyVersion: 1.453.4
generationVersion: 2.474.6
releaseVersion: 0.0.1-alpha.66
configChecksum: 43c79740f48a0f0f42b7bf154e8e62d9
published: true
features:
typescript:
Expand Down
2 changes: 1 addition & 1 deletion .speakeasy/gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ generation:
oAuth2ClientCredentialsEnabled: false
oAuth2PasswordEnabled: false
typescript:
version: 0.0.1-alpha.65
version: 0.0.1-alpha.66
additionalDependencies:
dependencies: {}
devDependencies: {}
Expand Down
8 changes: 4 additions & 4 deletions .speakeasy/workflow.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
speakeasyVersion: 1.453.0
speakeasyVersion: 1.453.4
sources:
json-development:
sourceNamespace: json-development
sourceRevisionDigest: sha256:eddf97125e97f656ab4d89da841fe04f95ab4f59a7e3ac4ee2040c20af7e680b
sourceRevisionDigest: sha256:7088e2a32830e87a87b072d46b2dee4e8e3d5fb884ac998fc11475e7e89740c5
sourceBlobDigest: sha256:74dbf434630080e7ec5200c4f88da3354cab36f0589634a445bbb7cfc8659edc
tags:
- latest
Expand All @@ -11,10 +11,10 @@ targets:
my-first-target:
source: json-development
sourceNamespace: json-development
sourceRevisionDigest: sha256:eddf97125e97f656ab4d89da841fe04f95ab4f59a7e3ac4ee2040c20af7e680b
sourceRevisionDigest: sha256:7088e2a32830e87a87b072d46b2dee4e8e3d5fb884ac998fc11475e7e89740c5
sourceBlobDigest: sha256:74dbf434630080e7ec5200c4f88da3354cab36f0589634a445bbb7cfc8659edc
codeSamplesNamespace: code-samples-typescript
codeSamplesRevisionDigest: sha256:5ecc4e7f0dae1957a48ed1f70f8997a34ebfbefc750f666c166acd3e14ebec70
codeSamplesRevisionDigest: sha256:f417010d423c4212bb689e1edcaa6c2ba24ea196ffa10bf2db73c2fa6382cc8a
workflow:
workflowVersion: 1.0.0
speakeasyVersion: latest
Expand Down
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{
"name": "@novu/api",
"version": "0.0.1-alpha.65",
"version": "0.0.1-alpha.66",
"exports": {
".": "./src/index.ts",
"./models/errors": "./src/models/errors/index.ts",
Expand Down
58 changes: 2 additions & 56 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@novu/api",
"version": "0.0.1-alpha.65",
"version": "0.0.1-alpha.66",
"author": "Novu",
"main": "./index.js",
"sideEffects": false,
Expand All @@ -10,8 +10,6 @@
"prepublishOnly": "npm run build"
},
"peerDependencies": {
"react": "^18 || ^19",
"react-dom": "^18 || ^19",
"zod": ">= 3"
},
"devDependencies": {
Expand Down
20 changes: 20 additions & 0 deletions src/hooks/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
BeforeCreateRequestHook,
BeforeRequestContext,
BeforeRequestHook,
Hook,
Hooks,
SDKInitHook,
SDKInitOptions,
Expand All @@ -27,6 +28,25 @@ export class SDKHooks implements Hooks {
afterErrorHooks: AfterErrorHook[] = [];

constructor() {
const presetHooks: Array<Hook> = [];

for (const hook of presetHooks) {
if ("sdkInit" in hook) {
this.registerSDKInitHook(hook);
}
if ("beforeCreateRequest" in hook) {
this.registerBeforeCreateRequestHook(hook);
}
if ("beforeRequest" in hook) {
this.registerBeforeRequestHook(hook);
}
if ("afterSuccess" in hook) {
this.registerAfterSuccessHook(hook);
}
if ("afterError" in hook) {
this.registerAfterErrorHook(hook);
}
}
initHooks(this);
}

Expand Down
21 changes: 5 additions & 16 deletions src/hooks/novu-custom-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,18 @@ export class NovuCustomHook
}

async afterSuccess(_hookCtx: AfterSuccessContext, response: Response): Promise<Response> {
if (!response || !response.ok) {
const responseAsText = await response.clone().text();
if (!responseAsText || responseAsText =='') {
return response;
}
const jsonResponse = await response.clone().json();

const clonedResponse = response.clone();
const contentLength = clonedResponse.headers.get('Content-Length');
if (!contentLength || contentLength === '0') {
return response;
}

let jsonResponse;
try {
jsonResponse = await clonedResponse.json();
} catch (error) {
return response;
}

if (jsonResponse && typeof jsonResponse === 'object' && Object.keys(jsonResponse).length === 1 && jsonResponse.data) {
if (jsonResponse && Object.keys(jsonResponse).length === 1 && jsonResponse.data) {
return new Response(JSON.stringify(jsonResponse.data), {
status: response.status,
statusText: response.statusText,
headers: response.headers,
});
}); // Return the new Response object
}

return response;
Expand Down
7 changes: 7 additions & 0 deletions src/hooks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,10 @@ export interface Hooks {
/** Registers a hook to be used by the SDK for the after error event. */
registerAfterErrorHook(hook: AfterErrorHook): void;
}

export type Hook =
| SDKInitHook
| BeforeCreateRequestHook
| BeforeRequestHook
| AfterSuccessHook
| AfterErrorHook;
6 changes: 3 additions & 3 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function serverURLFromOptions(options: SDKOptions): URL | null {
export const SDK_METADATA = {
language: "typescript",
openapiDocVersion: "1.0",
sdkVersion: "0.0.1-alpha.65",
genVersion: "2.472.1",
userAgent: "speakeasy-sdk/typescript 0.0.1-alpha.65 2.472.1 1.0 @novu/api",
sdkVersion: "0.0.1-alpha.66",
genVersion: "2.474.6",
userAgent: "speakeasy-sdk/typescript 0.0.1-alpha.66 2.474.6 1.0 @novu/api",
} as const;

0 comments on commit e9af132

Please sign in to comment.