Skip to content

Commit

Permalink
Merge pull request #54 from ExWeiv/dev
Browse files Browse the repository at this point in the history
BUG Fixes to Secrets for WeivDataURIs
  • Loading branch information
loeiks authored May 13, 2024
2 parents 978edf9 + 40dc262 commit 6994c3b
Show file tree
Hide file tree
Showing 74 changed files with 283 additions and 256 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ In this file you can find what's changed in each version. (Versions with -dev, -

---

### 3.0.4

- BUG Fixes

### 3.0.3

- Default cache time for MongoClient changed to 10 min (it was 5 min)
Expand Down
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

<br>

- [Docs](https://weivdata.exweiv.com/)
- [API Reference](https://weiv-data.apps.exweiv.com/)
- [Install - NPM](https://www.npmjs.com/package/@exweiv/weiv-data)
- [Changelog](https://github.com/ExWeiv/weiv-data/blob/main/CHANGELOG.md)

<br>

---

<br>

Powered by ExWeiv Apps
[Kolay Gelsin](https://medium.com/the-optimists-daily/kolay-gelsin-a-turkish-expression-we-should-all-know-and-use-83fc1207ae5d) 💜

<img src="https://static.wixstatic.com/shapes/510eca_43b52053314d4ad689df41b907baef42.svg" width="120px">
<img src="https://static.wixstatic.com/media/510eca_399a582544de4cb2b958ce934578097f~mv2.png">
8 changes: 3 additions & 5 deletions app/DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,12 @@ MongoClients are cached and saved so after cold start it will be cached for 5 mi

Ask for help, give feedback or anything else? [email protected]

- [Docs](https://weivdata.exweiv.com/)
- [API Reference](https://weiv-data.apps.exweiv.com/)
- [Install - NPM](https://www.npmjs.com/package/@exweiv/weiv-data)
- [Changelog](https://github.com/ExWeiv/weiv-data/blob/main/CHANGELOG.md)

---

<br>

Powered by ExWeiv Apps
[Kolay Gelsin](https://medium.com/the-optimists-daily/kolay-gelsin-a-turkish-expression-we-should-all-know-and-use-83fc1207ae5d) 💜

<img src="https://static.wixstatic.com/shapes/510eca_43b52053314d4ad689df41b907baef42.svg" width="120px">
<img src="https://static.wixstatic.com/media/510eca_399a582544de4cb2b958ce934578097f~mv2.png">
8 changes: 4 additions & 4 deletions app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ You can use this library whenever you want it's all up to you. But we recommend

> You can also use native mongodb collection cursor with this library. Learn more about `.native` function in docs.
You can find more info at our **[Official Docs](https://weivdata.exweiv.com/)**.
You can find more info at our **[API Reference](https://weiv-data.apps.exweiv.com/)**.

[Read changelog here](https://github.com/ExWeiv/weiv-data/blob/main/CHANGELOG.md)

---

Please report BUGs and leave your feedbacks. [email protected] or you can create an issue in [GitHub repo](https://github.com/ExWeiv/weiv-data/issues).

<br>
---

Powered by ExWeiv Apps
[Kolay Gelsin](https://medium.com/the-optimists-daily/kolay-gelsin-a-turkish-expression-we-should-all-know-and-use-83fc1207ae5d) 💜

<img src="https://static.wixstatic.com/shapes/510eca_43b52053314d4ad689df41b907baef42.svg" width="120px">
<img src="https://static.wixstatic.com/media/510eca_399a582544de4cb2b958ce934578097f~mv2.png">
20 changes: 17 additions & 3 deletions app/lib/Helpers/secret_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,31 @@ const cache = new node_cache_1.default();
const getSecretValue = wixAuth.elevate(wix_secrets_backend_v2_1.secrets.getSecretValue);
async function getCachedSecret(secretName, parse) {
try {
if (typeof secretName !== "string") {
throw new Error(`Secret Name param is not string!`);
}
let secret = cache.get(secretName);
if (secret === undefined) {
const { value } = await getSecretValue(secretName);
if (parse === true) {
const objectSecret = await JSON.parse(value);
secret = objectSecret;
let objectSecret;
try {
objectSecret = JSON.parse(value);
}
catch (err) {
throw new Error(`Failed to parse JSON for secret '${secretName}': ${err}`);
}
if (typeof objectSecret === 'object' && objectSecret !== null) {
secret = objectSecret;
}
else {
throw new Error(`Parsed JSON is not an object for secret '${secretName}'`);
}
}
else {
secret = value;
}
cache.set(secretName, value, 60 * 10);
cache.set(secretName, secret, 60 * 6);
}
return secret;
}
Expand Down
4 changes: 2 additions & 2 deletions app/package-lock.json

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

4 changes: 2 additions & 2 deletions app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exweiv/weiv-data",
"version": "3.0.3",
"version": "3.0.4",
"description": "Custom API Library for Wix sites to connect MongoDB. Designed to easily switch from wix-data APIs.",
"main": "./lib/index.js",
"files": [
Expand Down Expand Up @@ -55,7 +55,7 @@
"url": "[email protected]:ExWeiv/weiv-data.git",
"type": "git"
},
"homepage": "https://weivdata.exweiv.com/",
"homepage": "https://weiv-data.apps.exweiv.com/",
"bugs": {
"url": "https://github.com/ExWeiv/weiv-data/issues",
"email": "[email protected]"
Expand Down
21 changes: 18 additions & 3 deletions app/src/Helpers/secret_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ type SecretResponse<T> = T extends "URI" ? { visitor: string, member: string, ad
*/
export async function getCachedSecret<URI>(secretName: string, parse?: boolean): Promise<SecretResponse<URI>> {
try {
if (typeof secretName !== "string") {
throw new Error(`Secret Name param is not string!`);
}

// Try to get the secret from the cache
let secret: any = cache.get(secretName);

Expand All @@ -27,14 +31,25 @@ export async function getCachedSecret<URI>(secretName: string, parse?: boolean):
const { value } = await getSecretValue(secretName);

if (parse === true) {
const objectSecret: object = await JSON.parse(value);
secret = objectSecret;
// Parse the JSON safely
let objectSecret;
try {
objectSecret = JSON.parse(value);
} catch (err) {
throw new Error(`Failed to parse JSON for secret '${secretName}': ${err}`);
}

if (typeof objectSecret === 'object' && objectSecret !== null) {
secret = objectSecret;
} else {
throw new Error(`Parsed JSON is not an object for secret '${secretName}'`);
}
} else {
secret = value;
}

// Set the secret in the cache with a specific TTL (e.g., 1 hour)
cache.set(secretName, value, 60 * 10);
cache.set(secretName, secret, 60 * 6);
}

return secret;
Expand Down
Loading

0 comments on commit 6994c3b

Please sign in to comment.