Skip to content

Commit

Permalink
fix: promiser wrapper implementation in all find functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajat Kaushik committed Jun 28, 2023
1 parent e8b6f4e commit d701952
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 112 deletions.
127 changes: 64 additions & 63 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,65 +1,66 @@
{
"name": "@cosmicjs/sdk",
"version": "1.0.6",
"description": "The official client module for Cosmic. This module helps you easily add dynamic content to your website or application using the Cosmic headless CMS.",
"keywords": [
"headlesscms",
"cms",
"node",
"content",
"api",
"react",
"reactjs",
"vue",
"vuejs",
"nextjs"
],
"homepage": "https://www.cosmicjs.com",
"bugs": {
"url": "https://github.com/cosmicjs/cosmic-sdk-js/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/cosmicjs/cosmic-sdk-js.git"
},
"license": "MIT",
"author": "Tony Spiro <[email protected]>",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts --minify",
"tsc:lint": "tsc",
"lint": "eslint --fix 'src/**/*.{ts,js}'",
"format": "prettier --write './src/**/*.{ts,js}'",
"release": "npm run build && changeset publish",
"prepare": "husky install",
"prepublishOnly": "npm run build"
},
"dependencies": {
"axios": "^1.3.4",
"form-data": "^4.0.0"
},
"devDependencies": {
"@changesets/cli": "^2.26.1",
"@semantic-release/changelog": "^6.0.2",
"@types/node": "^18.15.5",
"@typescript-eslint/eslint-plugin": "^5.56.0",
"eslint": "^8.36.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.8.0",
"eslint-import-resolver-typescript": "^3.5.3",
"eslint-plugin-prettier": "^4.2.1",
"husky": "^8.0.3",
"lint-staged": "^13.2.0",
"prettier": "^2.8.6",
"tsup": "^6.7.0",
"typescript": "^5.0.2"
},
"lint-staged": {
"src/**/*.{ts,js}": [
"npx prettier --write",
"npx eslint --fix"
]
}
"name": "@cosmicjs/sdk",
"version": "1.0.6",
"description": "The official client module for Cosmic. This module helps you easily add dynamic content to your website or application using the Cosmic headless CMS.",
"keywords": [
"headlesscms",
"cms",
"node",
"content",
"api",
"react",
"reactjs",
"vue",
"vuejs",
"nextjs"
],
"homepage": "https://www.cosmicjs.com",
"bugs": {
"url": "https://github.com/cosmicjs/cosmic-sdk-js/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/cosmicjs/cosmic-sdk-js.git"
},
"license": "MIT",
"author": "Tony Spiro <[email protected]>",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts --minify",
"tsc:lint": "tsc",
"lint": "eslint --fix 'src/**/*.{ts,js}'",
"format": "prettier --write './src/**/*.{ts,js}'",
"release": "npm run build && changeset publish",
"prepare": "husky install",
"prepublishOnly": "npm run build",
"test": "tsc-watch --project . --outDir ./dist --onSuccess \"nodemon ./test.ts\""
},
"dependencies": {
"axios": "^1.3.4",
"form-data": "^4.0.0"
},
"devDependencies": {
"@changesets/cli": "^2.26.1",
"@semantic-release/changelog": "^6.0.2",
"@types/node": "^18.15.5",
"@typescript-eslint/eslint-plugin": "^5.56.0",
"eslint": "^8.36.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.8.0",
"eslint-import-resolver-typescript": "^3.5.3",
"eslint-plugin-prettier": "^4.2.1",
"husky": "^8.0.3",
"lint-staged": "^13.2.0",
"prettier": "^2.8.6",
"tsup": "^6.7.0",
"typescript": "^5.0.2"
},
"lint-staged": {
"src/**/*.{ts,js}": [
"npx prettier --write",
"npx eslint --fix"
]
}
}
14 changes: 4 additions & 10 deletions src/clients/bucket/media/lib/find.chaining.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PromiseFnType } from '../../../../types/promise.types';
import { promiser } from '../../../../utils/request.promiser';
import { promiserTryCatchWrapper } from '../../../../utils/request.promiser';
import MethodChaining from '../../lib/methodChaining';

export default class FindChaining extends MethodChaining {
Expand All @@ -12,14 +12,8 @@ export default class FindChaining extends MethodChaining {
onFulfilled?: PromiseFnType<FulfilledResult>,
onRejected?: PromiseFnType<RejectedResult>
) {
promiser(this.endpoint)
.then((res) => onFulfilled?.(res))
.catch((err) => {
if (typeof onRejected === 'function') {
onRejected?.(err);
} else {
onFulfilled?.(null);
}
});
await promiserTryCatchWrapper(this.endpoint, onRejected, (result) =>
onFulfilled?.(result)
);
}
}
14 changes: 4 additions & 10 deletions src/clients/bucket/objects/lib/find.chaining.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PromiseFnType } from '../../../../types/promise.types';
import { promiser } from '../../../../utils/request.promiser';
import { promiserTryCatchWrapper } from '../../../../utils/request.promiser';
import Chaining from './chaining';

export default class FindChaining extends Chaining {
Expand All @@ -12,14 +12,8 @@ export default class FindChaining extends Chaining {
onFulfilled?: PromiseFnType<FulfilledResult>,
onRejected?: PromiseFnType<RejectedResult>
) {
promiser(this.endpoint)
.then((res) => onFulfilled?.(res))
.catch((err) => {
if (typeof onRejected === 'function') {
onRejected?.(err);
} else {
onFulfilled?.(null);
}
});
await promiserTryCatchWrapper(this.endpoint, onRejected, (res) =>
onFulfilled?.(res)
);
}
}
18 changes: 5 additions & 13 deletions src/clients/bucket/objects/lib/findOne.chaining.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import { PromiseFnType } from '../../../../types/promise.types';
import { promiser } from '../../../../utils/request.promiser';
import { promiserTryCatchWrapper } from '../../../../utils/request.promiser';
import Chaining from './chaining';

export default class FindOneChaining extends Chaining {
async then<FulfilledResult = any, RejectedResult = never>(
onFulfilled?: PromiseFnType<FulfilledResult>,
onRejected?: PromiseFnType<RejectedResult>
) {
promiser(this.endpoint)
.then((res: any) =>
onFulfilled?.({
object: res.objects && res.objects.length ? res.objects[0] : null,
})
)
.catch((err) => {
if (typeof onRejected === 'function') {
onRejected?.(err);
} else {
onFulfilled?.(null);
}
await promiserTryCatchWrapper(this.endpoint, onRejected, (res) => {
onFulfilled?.({
object: res.objects && res.objects.length ? res.objects[0] : null,
});
});
}
}
34 changes: 18 additions & 16 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,32 @@
// readKey: 'xjwpAMIEFHX1EtpA6ZN4KlaMSRVDbz6L2tPg39X2rhT7wKM51s',
// });

// cosmic.media
// .findOne({ original_name: 'jimmy-philadelphie-unsplash.' })
// .props('url,imgix_url,name')
// cosmic.objects
// .findOne({
// type: 'itineraires',
// slug: 'voyage-sportif-ultime-les-4-sports-en-4-jous',
// })
// .props('slug,title,metadata')
// .depth(1)
// .then((data) => console.log(data, 'CHAIN -> THEN SUCCESSFUL'))
// .catch((err) =>
// console.log('CHAIN ---> ERROR FROM THE CHAIN METHOD -->', err)
// );
// .catch((err) => console.log('CHAIN ---> ERROR FROM THE CHAIN METHOD', err));

// async function run() {
// let posts;
// try {
// posts = await cosmic.media
// .findOne({ original_name: 'jimmy-philadelphie-unsplash.jp' })
// .props('url,imgix_url,name');
// posts = await cosmic.objects
// .findOne({
// type: 'itineraires',
// slug: 'voyage-sportif-ultime-les-4-sports-en-4-jour',
// })
// .props('slug,title,metadata')
// .depth(1);
// console.log(posts, 'TRYCATCH ---> SUCCESSFUL!');
// return posts;
// } catch (e) {
// console.log('TRYCATCH --> CATCH BLOCK ERROR CAUGHT SUCCESSFULLY', e);
// throw e;
// return null;
// }
// }

// console.log(
// 'TEST LOG _>',
// run()
// .then(() => console.log('TRYCATCH -> CHAINED THEN WORKED'))
// .catch((e) => console.log('TRYCATCH -> CHAINED ERROR WORKED', e))
// );
// run();

0 comments on commit d701952

Please sign in to comment.