@openagenda/sdk-js
v2.0.3
Published
OpenAgenda SDK for JavaScript in the browser and Node.js.
Downloads
81
Readme
OpenAgenda SDK for JavaScript in the browser and Node.js.
Summary
Installation
yarn add @openagenda/sdk-js
or
npm i @openagenda/sdk-js
Configuration
In the following examples we use async
/ await
, you can use the promises if your environment does not allow it.
Before any operation you have to connect, the token is refreshed automatically as soon as it is necessary.
const OaSdk = require('@openagenda/sdk-js');
const oa = new OaSdk({
publicKey: 'YOUR-PUBLIC-KEY',
secretKey: 'YOUR-PRIVATE-KEY'
});
await oa.connect();
The public key is used for get
method.
The private key is used for all create
, update
, and delete
methods.
API
For more information about data formats please refer to the API documentation on our help center.
Events
const agendaUid = 12345678;
const eventUid = 87654321;
const event = await oa.events.get(agendaUid, eventUid);
const eventUid = 87654321;
const event = await oa.events.list(agendaUid, { sort: 'updatedAt.desc' });
const agendaUid = 12345678;
const event = await oa.events.create(agendaUid, {
slug: 'a-title',
title: {
fr: 'Un titre',
en: 'A title'
},
description: {
fr: 'La description de votre événement',
en: 'The description of your event'
},
locationUid: 78372099,
timings: [
{
begin: moment(),
end: moment().add(1, 'hour')
}, {
begin: moment().add(1, 'day'),
end: moment().add(1, 'day').add(1, 'hour')
}
]
});
In this example we use moment for manage the timings, but you can also use the native Date
object.
const agendaUid = 12345678;
const eventUid = 87654321;
const updatedEvent = await oa.events.update(
agendaUid,
eventUid,
{
...event,
title: {
fr: 'Titre mise à jour',
en: 'Updated title'
}
}
);
const agendaUid = 12345678;
const eventUid = 87654321;
const patchedEvent = await oa.events.patch(
agendaUid,
eventUid,
{
title: {
fr: 'Titre mise à jour',
en: 'Updated title'
}
}
);
const agendaUid = 12345678;
const eventUid = 87654321;
const deletedEvent = await oa.events.delete(agendaUid, eventUid);
Locations
const agendaUid = 12345678;
const locationUid = 87654321;
const location = await oa.locations.get(agendaUid, locationUid);
const agendaUid = 12345678;
const location = await oa.locations.list(agendaUid, { sort: 'updatedAt.desc' });
const agendaUid = 12345678;
const location = await oa.locations.create(agendaUid, {
name: 'Gare Meuse TGV',
address: 'Lieu dit Le Cugnet, 55220 Les Trois-Domaines',
latitude: 48.9736458,
longitude: 5.2723537
});
const agendaUid = 12345678;
const locationUid = 87654321;
const location = await oa.locations.update(agendaUid, locationUid, {
...location,
address: 'Lieu dit Le Cugnet, 55220 Les Trois-Domaines'
});
const agendaUid = 12345678;
const locationUid = 87654321;
const location = await oa.locations.patch(agendaUid, locationUid, {
address: 'Lieu dit Le Cugnet, 55220 Les Trois-Domaines',
});
const agendaUid = 12345678;
const locationUid = 87654321;
const location = await oa.locations.delete(agendaUid, locationUid);
Errors
Whatever the method if the error comes from the API the error will be in error.response
and its JSON content
in error.response.data
.
If you use async
/ await
then you can use try
/ catch
otherwise you will have to use .catch
.
async / await example:
try {
await oa.events.create(12345678, {
slug: 'a-title',
description: {
fr: 'La description de votre événement',
en: 'The description of your event'
},
locationUid: 87654321,
timings: [
{
begin: moment(),
end: moment().add(1, 'hour')
}, {
begin: moment().add(1, 'day'),
end: moment().add(1, 'day').add(1, 'hour')
}
]
});
} catch (e) {
expect(e.response.data).to.be.eql({
errors: [
{
field: 'title',
code: 'required',
message: 'at least one language entry is required'
}
]
});
}
Promise example:
oa.events.create(12345678, {
slug: 'a-title',
description: {
fr: 'La description de votre événement',
en: 'The description of your event'
},
locationUid: 87654321,
timings: [
{
begin: moment(),
end: moment().add(1, 'hour')
}, {
begin: moment().add(1, 'day'),
end: moment().add(1, 'day').add(1, 'hour')
}
]
})
.then(result => {
//
})
.catch(error => {
expect(error.response.data).to.be.eql({
errors: [
{
field: 'title',
code: 'required',
message: 'at least one language entry is required'
}
]
});
});