0% found this document useful (0 votes)
7 views2 pages

May 09 10 - 10 AM

The document defines a TypeScript interface for retry options and a fake fetch function that simulates API responses. It includes a retry mechanism for handling failed requests and a logging feature to track fetch operations. Additionally, it provides a TypeScript configuration for compiling the code with strict type checks and ES6 module syntax.

Uploaded by

dekij98579
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

May 09 10 - 10 AM

The document defines a TypeScript interface for retry options and a fake fetch function that simulates API responses. It includes a retry mechanism for handling failed requests and a logging feature to track fetch operations. Additionally, it provides a TypeScript configuration for compiling the code with strict type checks and ES6 module syntax.

Uploaded by

dekij98579
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

interface RetryOptions {

retryCount: number
retryDelay?: number
retryOn?: number[]
}

type ResponseFake = {
body: object,
status: number
};

type FetchFake = {
(url:string, len?:number):Promise<ResponseFake>;
};

const f1:FetchFake= async (url:string, len=10): Promise<ResponseFake> => {


let result = '';
let characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let rndValue = Math.random();
console.log("rnd_value "+rndValue);
if (rndValue < 0.5)
{
const fakeBadResponse:ResponseFake = {
body: {"result": null},
status: 500
};
return new Promise( (resolve) => resolve(fakeBadResponse));
}

for ( let i = 0; i < len; i++ ) {


result += characters.charAt(Math.floor(Math.random() * characters.length));
}
console.log(result);
const fakeGoodResponse:ResponseFake = {
body: {"result": result},
status: 200
};
return new Promise( (resolve) => resolve(fakeGoodResponse));
}

const withRetry = (fetchFake: FetchFake , options: RetryOptions) => {


const { retryCount = 1, retryDelay = 1000, retryOn = [500] } = options;
let response: ResponseFake;
return async (url:string, len:number=10): Promise<ResponseFake> => {
let retry = 0;
while (retry < retryCount) {
response = await fetchFake(url,len);
console.log("retry....");
if (retryOn.includes(response.status)) {
retry++;
await new Promise((resolve) => setTimeout(resolve,
retryDelay*2**retry));
}
else {
return response;
}
}
return response;
}
}

interface Logger {
log: (message: string) => void;
info: (message: string) => void;
error: (message: string) => void;
warn: (message: string) => void;
}

const withLogger = (fetchFake: FetchFake, logger: Logger = console): FetchFake => {


return async (url:string, len:number=10) => {
const response = await fetchFake(url, len);
logger.info(`fetching ${url} and got ${response.status}`);
return response;
}
}

let out=withLogger(withRetry(f1,{ retryCount: 3 }))


("ciao").then((value)=>console.log(value.body)).catch((err)=>console.log(err));

/*TSCONFIG*/

{
"compilerOptions": {
"target": "ES6", // Output ES6 code
"module": "ES6", // Use ES6 module syntax (or "commonjs" if
you're using Node)
"strict": true, // Enable all strict type checks
"lib": ["dom","ES2016"],
"esModuleInterop": true, // Enable default import compatibility
"moduleResolution": "node", // Helps with resolving modules like Node.js
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true // Skip type checking of declaration files
for faster builds
},
"include": ["./**/*"], // Include all TS files in src/
"exclude": ["node_modules", "dist"] // Don't compile these folders
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy