-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
71 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,48 @@ | ||
import { Storage } from 'aws-amplify'; | ||
import { Storage } from 'aws-amplify' | ||
|
||
export const noop = async () => { | ||
undefined | ||
} | ||
|
||
export const noop = async () => { undefined } | ||
|
||
export const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); | ||
export const sleep = (ms: number) => | ||
new Promise(resolve => setTimeout(resolve, ms)) | ||
|
||
export const retry = async ({ fn = noop, retries = 120, delay = 4000, err = '' }) => { | ||
const attempt = async (remainingRetries: number, lastError = null): Promise<any> => { | ||
export const retry = async ({ | ||
fn = noop, | ||
retries = 120, | ||
delay = 4000, | ||
err = '', | ||
}) => { | ||
const attempt = async ( | ||
remainingRetries: number, | ||
lastError = null, | ||
): Promise<any> => { | ||
if (remainingRetries <= 0) { | ||
throw new Error(`RemainingRetries ${retries}, ${lastError}`); | ||
throw new Error(`RemainingRetries ${retries}, ${lastError}`) | ||
} | ||
try { | ||
return await fn(); | ||
return await fn() | ||
} catch (error) { | ||
await sleep(delay); | ||
return attempt(remainingRetries - 1); | ||
await sleep(delay) | ||
return attempt(remainingRetries - 1) | ||
} | ||
}; | ||
} | ||
|
||
return attempt(retries); | ||
return attempt(retries) | ||
} | ||
|
||
|
||
export const getData = async (key: string) => { | ||
export const getData = async (key: string, prefix = '_edited') => { | ||
try { | ||
const config = { | ||
validateObjectExistence: true, | ||
download: false, expires: 3600 | ||
download: false, | ||
expires: 3600, | ||
} | ||
const [name, format] = key.split('.') | ||
const newKey = `${name}_edited.${format}` | ||
const newKey = `${name}${prefix}.${format}` | ||
console.log({ newKey }) | ||
return await Storage.get(newKey, config) | ||
} catch (error) { | ||
throw new Error("No data yet"); | ||
throw new Error('No data yet') | ||
} | ||
} | ||
} |