Skip to content

Instantly share code, notes, and snippets.

@malihassan20
Last active January 18, 2018 18:33
Show Gist options
  • Save malihassan20/e1bd7abeb5e6fac0d02fca0d0a22bb7f to your computer and use it in GitHub Desktop.
Save malihassan20/e1bd7abeb5e6fac0d02fca0d0a22bb7f to your computer and use it in GitHub Desktop.
nextJs-tour-diary-app\cosmic\cosmic.js
import Cosmic from 'cosmicjs';
import config from './config';
export default function cosmic(type, params) {
if (type === 'GET_TYPE') {
return new Promise((resolve, reject) => {
Cosmic.getObjectType(config, params, (err, res) => {
if (!err) {
resolve(res.objects.all);
} else {
reject(err);
}
});
});
} else if (type === 'SEARCH_BY_TYPE') {
return new Promise((resolve, reject) => {
Cosmic.getObjectsBySearch(config, params, (err, res) => {
if (!err) {
resolve(res.objects.all);
} else {
reject(err);
}
});
});
} else if (type === 'ADD') {
return new Promise((resolve, reject) => {
Cosmic.addObject(config, params, (err, res) => {
if (!err) {
resolve(res);
} else {
reject(err);
}
});
});
} else if (type === 'EDIT') {
return new Promise((resolve, reject) => {
Cosmic.editObject(config, params, (err, res) => {
if (!err) {
resolve(res);
} else {
reject(err);
}
});
});
} else if (type === 'DELETE') {
return new Promise((resolve, reject) => {
Cosmic.deleteObject(config, params, (err, res) => {
if (!err) {
resolve(res);
} else {
reject(err);
}
});
});
} else if (type === 'ADD_MEDIA') {
return new Promise((resolve, reject) => {
Cosmic.addMedia(config, params, (err, res) => {
if (!err) {
resolve(res);
} else {
reject(err);
}
});
});
} else if (type === 'DELETE_MEDIA') {
return new Promise((resolve, reject) => {
Cosmic.deleteMedia(config, params, (err, res) => {
if (!err) {
resolve(res);
} else {
reject(err);
}
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment