Skip to content

Commit

Permalink
Merge pull request #9 from Teneff/bugfix-issue-#8
Browse files Browse the repository at this point in the history
Bugfix issue #8
  • Loading branch information
Teneff committed May 23, 2024
2 parents 3c0dc42 + bba4eb4 commit 2a6df73
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@teneff/with-retry",
"version": "1.1.2",
"version": "1.1.3",
"description": "Decorator for retrying async operations",
"main": "index.js",
"types": "index.d.ts",
Expand Down
7 changes: 7 additions & 0 deletions src/decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class Example {
getData2(): Promise<string> {
return this.mockCallback("arg1", "arg2");
}

@withRetry({
maxCalls: 4,
})
async getData3() {
console.log('3')
}
}

describe("decorator", () => {
Expand Down
6 changes: 3 additions & 3 deletions src/decorator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import withRetry from "./withRetry";

export default function <T>(...args: Parameters<typeof withRetry>) {
return function <K extends string>(
target: { [key in K]: (...args: any[]) => Promise<T> },
return function <K extends string, V extends Record<K, (...args: any[]) => Promise<T>>>(
target: V,
propertyKey: K,
descriptor?: TypedPropertyDescriptor<() => Promise<T>>
descriptor?: TypedPropertyDescriptor<V[K]>
): any {
const method = target[propertyKey];
return Object.assign({}, descriptor, {
Expand Down
59 changes: 59 additions & 0 deletions src/decorator2.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import withRetry from "./decorator";

type Config = {
method: string;
url: string;
headers: {
"user-api-key": string;
};
};

class ErrorOnWhichWeShouldRetry extends Error {}

class Example {
configService = {
get: <T extends string>(v: T): T => v,
};
httpService = {
axiosRef: {
request: async (config: Config) => ({
data: JSON.stringify(config),
}),
},
};

@withRetry({
errors: [ErrorOnWhichWeShouldRetry],
maxCalls: 10,
delay: 1000,
})
async acceptCredentialOffer(
userCredentialOfferId: string,
userApiKey: string,
subjectId: string
) {
const config = {
method: "post",
url: `${this.configService.get<string>(
"prism.prismApiUrl"
)}/accept-credential-offer/${userCredentialOfferId}?subjectId=${subjectId}`,
headers: {
"user-api-key": userApiKey,
},
};

return this.httpService.axiosRef.request(config);
}
}

describe("Example", () => {
describe("acceptCredentialOffer", () => {
it("should acceptCredentialOffer", () => {
return expect(
new Example().acceptCredentialOffer("a", "b", "c")
).resolves.toEqual({
data: '{\"method\":\"post\",\"url\":\"prism.prismApiUrl/accept-credential-offer/a?subjectId=c\",\"headers\":{\"user-api-key\":\"b\"}}',
});
});
});
});

0 comments on commit 2a6df73

Please sign in to comment.