Skip to content

Commit

Permalink
add idempotency test endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
tatarco committed Jan 13, 2025
1 parent 8d8c2b9 commit 9d835d2
Show file tree
Hide file tree
Showing 20 changed files with 92 additions and 63 deletions.
4 changes: 2 additions & 2 deletions .speakeasy/gen.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ management:
docVersion: "1.0"
speakeasyVersion: 1.468.2
generationVersion: 2.493.4
releaseVersion: 0.0.1-alpha.175
configChecksum: e970cd92643031a3ef97fb65d9eeb0a7
releaseVersion: 0.0.1-alpha.177
configChecksum: c2e4a5caaaf4455720deb35c1351dec7
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.175
version: 0.0.1-alpha.177
additionalDependencies:
dependencies: {}
devDependencies: {}
Expand Down
6 changes: 3 additions & 3 deletions .speakeasy/workflow.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ speakeasyVersion: 1.468.2
sources:
json-development:
sourceNamespace: json-development
sourceRevisionDigest: sha256:580c6bd42fd13e170c6b680229e820d3d6438b664952081c19820aad511505d6
sourceRevisionDigest: sha256:d73bcd91d441a1079337d9638dc6a348c9d4060fd96133106686615dec129f1d
sourceBlobDigest: sha256:ab8e8daf7f729d09d69a3060980440570217f12d6d03cc4f414d27ef43effbac
tags:
- latest
Expand All @@ -11,10 +11,10 @@ targets:
my-first-target:
source: json-development
sourceNamespace: json-development
sourceRevisionDigest: sha256:580c6bd42fd13e170c6b680229e820d3d6438b664952081c19820aad511505d6
sourceRevisionDigest: sha256:d73bcd91d441a1079337d9638dc6a348c9d4060fd96133106686615dec129f1d
sourceBlobDigest: sha256:ab8e8daf7f729d09d69a3060980440570217f12d6d03cc4f414d27ef43effbac
codeSamplesNamespace: code-samples-typescript
codeSamplesRevisionDigest: sha256:61a17f54ae320df141a590b1d4e21f73dec5ac5e933d2212e984d596eb71ecfb
codeSamplesRevisionDigest: sha256:a813b8e83186682107926592614baf2692f765f52dccaee253a142b2b554bdd2
workflow:
workflowVersion: 1.0.0
speakeasyVersion: latest
Expand Down
6 changes: 3 additions & 3 deletions docs/models/components/messageresponsedto.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ let value: MessageResponseDto = {
organizationId: "<id>",
environmentId: "<id>",
deleted: false,
createdAt: "1710353524711",
updatedAt: "1736731931417",
createdAt: "1710358861872",
updatedAt: "1736737268578",
},
createdAt: "1709133934542",
createdAt: "1709139271702",
content: "<value>",
transactionId: "<id>",
channel: "push",
Expand Down
4 changes: 2 additions & 2 deletions docs/models/components/subscriberresponsedto.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ let value: SubscriberResponseDto = {
organizationId: "<id>",
environmentId: "<id>",
deleted: false,
createdAt: "1714377034712",
updatedAt: "1736732143496",
createdAt: "1714382371844",
updatedAt: "1736737480628",
};
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ let value: SubscribersControllerCreateSubscriberResponse = {
organizationId: "<id>",
environmentId: "<id>",
deleted: false,
createdAt: "1718305925812",
updatedAt: "1736701604627",
createdAt: "1718311262921",
updatedAt: "1736706941736",
},
};
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ let value: SubscribersControllerGetSubscriberResponse = {
organizationId: "<id>",
environmentId: "<id>",
deleted: false,
createdAt: "1719614303713",
updatedAt: "1736727859417",
createdAt: "1719619640825",
updatedAt: "1736733196529",
},
};
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ let value: SubscribersControllerListSubscribersResponse = {
organizationId: "<id>",
environmentId: "<id>",
deleted: false,
createdAt: "1709749936408",
updatedAt: "1736760365730",
createdAt: "1709755273508",
updatedAt: "1736765702830",
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ let value: SubscribersControllerListSubscribersResponseBody = {
organizationId: "<id>",
environmentId: "<id>",
deleted: false,
createdAt: "1730431410958",
updatedAt: "1736718618631",
createdAt: "1730436748059",
updatedAt: "1736723955732",
},
],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ let value: SubscribersControllerMarkActionAsSeenResponse = {
organizationId: "<id>",
environmentId: "<id>",
deleted: false,
createdAt: "1708291333127",
updatedAt: "1736751144993",
createdAt: "1708296670232",
updatedAt: "1736756482098",
},
createdAt: "1708259706687",
createdAt: "1708265043792",
content: "<value>",
transactionId: "<id>",
channel: "sms",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ let value: SubscribersControllerMarkMessagesAsResponse = {
organizationId: "<id>",
environmentId: "<id>",
deleted: false,
createdAt: "1725825570861",
updatedAt: "1736700631503",
createdAt: "1725830907980",
updatedAt: "1736705968622",
},
createdAt: "1719934723435",
createdAt: "1719940060548",
content: {
type: "button",
content: "<value>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ let value: SubscribersControllerModifySubscriberChannelResponse = {
organizationId: "<id>",
environmentId: "<id>",
deleted: false,
createdAt: "1707128417731",
updatedAt: "1736736355296",
createdAt: "1707133754846",
updatedAt: "1736741692411",
},
};
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ let value: SubscribersControllerUpdateSubscriberChannelResponse = {
organizationId: "<id>",
environmentId: "<id>",
deleted: false,
createdAt: "1726731031583",
updatedAt: "1736709808435",
createdAt: "1726736368698",
updatedAt: "1736715145550",
},
};
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ let value: SubscribersControllerUpdateSubscriberOnlineFlagResponse = {
organizationId: "<id>",
environmentId: "<id>",
deleted: false,
createdAt: "1711863771561",
updatedAt: "1736689886429",
createdAt: "1711869108678",
updatedAt: "1736695223546",
},
};
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ let value: SubscribersControllerUpdateSubscriberResponse = {
organizationId: "<id>",
environmentId: "<id>",
deleted: false,
createdAt: "1724706899715",
updatedAt: "1736731631755",
createdAt: "1724712236829",
updatedAt: "1736736968869",
},
};
```
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.175",
"version": "0.0.1-alpha.177",
"exports": {
".": "./src/index.ts",
"./models/errors": "./src/models/errors/index.ts",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@novu/api",
"version": "0.0.1-alpha.175",
"version": "0.0.1-alpha.177",
"author": "Novu",
"main": "./index.js",
"sideEffects": false,
Expand Down
77 changes: 53 additions & 24 deletions src/hooks/novu-custom-hook.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
import {AfterSuccessContext, AfterSuccessHook, BeforeRequestContext, BeforeRequestHook} from "./types";
import {RequestInput} from "../lib/http";
import {
AfterSuccessContext,
AfterSuccessHook,
BeforeCreateRequestHook,
BeforeRequestContext,
BeforeRequestHook,
HookContext
} from "./types";

export class NovuCustomHook
implements BeforeRequestHook, AfterSuccessHook
{
beforeRequest(_hookCtx: BeforeRequestContext, request: Request): Request {
this.addAuthHeader(request);
this.addIdempotencyHeader(request)
return request;
}
private addIdempotencyHeader(request: Request) {
implements BeforeRequestHook, AfterSuccessHook, BeforeCreateRequestHook {
beforeCreateRequest(_hookCtx: HookContext, input: RequestInput): RequestInput {
const idempotencyKey = 'Idempotency-Key';
const keyValue = request.headers.get(idempotencyKey);
request.headers.forEach((value, key) => {
console.log(`${key}: ${value}`);
});
if (!keyValue || keyValue==='' ) {
const newIdempotenceValue = this.generateIdempotencyKey();
request.headers.set(idempotencyKey, newIdempotenceValue)
}
const headers = input.options?.headers
if (!headers) {
return input
}
const updatedHeaders = this.updateHeaderValue(headers, idempotencyKey, this.generateIdempotencyKey)

return {...input, options: {headers: updatedHeaders}}
}
private generateIdempotencyKey(): string {
const timestamp = Date.now();
const randomString = Math.random().toString(36).substr(2, 9); // Generates a random alphanumeric string
return `${timestamp}-${randomString}`;
}
private addAuthHeader(request: Request) {

beforeRequest(_hookCtx: BeforeRequestContext, request: Request): Request {
const authKey = 'authorization';
const hasAuthorization = request.headers.has(authKey);
const apiKeyPrefix = 'ApiKey';
Expand All @@ -35,12 +32,20 @@ export class NovuCustomHook
request.headers.set(authKey, `${apiKeyPrefix} ${key}`)
}
}

return request;
}

private generateIdempotencyKey(): string {
const timestamp = Date.now();
const randomString = Math.random().toString(36).substr(2, 9); // Generates a random alphanumeric string
return `${timestamp}-${randomString}`;
}

async afterSuccess(_hookCtx: AfterSuccessContext, response: Response): Promise<Response> {
const responseAsText = await response.clone().text();
const contentType = response.headers.get('content-type') || '';
if (!responseAsText || responseAsText =='' || contentType.includes('text/html')) {
if (!responseAsText || responseAsText == '' || contentType.includes('text/html')) {
return response;
}
const jsonResponse = await response.clone().json();
Expand All @@ -55,4 +60,28 @@ export class NovuCustomHook

return response;
}

private updateHeaderValue(
headers: HeadersInit,
key: string,
defaultValueFunction: () => string
): Record<string, string> {
const headersRecord = this.convertToRecord(headers);

if (!(key in headersRecord) || headersRecord[key] === '') {
headersRecord[key] = defaultValueFunction();
}

return headersRecord;
}

private convertToRecord(headers: HeadersInit): Record<string, string> {
if (Array.isArray(headers)) {
return Object.fromEntries(headers);
} else if (headers instanceof Headers) {
return Object.fromEntries(headers.entries());
} else {
return {...headers};
}
}
}
4 changes: 2 additions & 2 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.175",
sdkVersion: "0.0.1-alpha.177",
genVersion: "2.493.4",
userAgent: "speakeasy-sdk/typescript 0.0.1-alpha.175 2.493.4 1.0 @novu/api",
userAgent: "speakeasy-sdk/typescript 0.0.1-alpha.177 2.493.4 1.0 @novu/api",
} as const;

0 comments on commit 9d835d2

Please sign in to comment.