JavaScript SDK Reference
The Freshcom API only accepts request body in JSON API format, however this format may not be the most convenient format to use for your client side application. This is why by default most method in the JavaScript SDK accept a unserialized object containing all the fields of a resource and will attempt to serialize it to JSON API format and then send the request using the serialized version of it. This unserialized object that the Javascript SDK accept is what we call the fields object. The fields object is a plain JavaScript object containing all the fields of a resource.
If the property of the fields object matches the following condition then those property will be serialized as the relationships of the resource:
- the property's value is an
Object
with atype
property; or - the property's value is an
Array
All other property will be serialized as the attributes of the resource. Below is an example of a valid fields object and its the JSON object after serialization.
Fields Object
Serialized
{
type: 'Product',
kind: 'simple',
name: 'Red Starship',
goods: {
id: '9fc8ee53-906f-48bd-a392-8b0709301699',
type: 'Stockable'
}
}
{
"data": {
"type": "Product",
"attributes": {
"kind": "simple",
"name": "Red Starship"
},
"relationships": {
"goods": {
"data": {
"id": "9fc8ee53-906f-48bd-a392-8b0709301699",
"type": "Stockable"
}
}
}
}
}
By default, all SDK methods that accepts a fields object will automatically serialize it before sending the request to the API, if you do not want the SDK to serialize the data for you and want to handle the serialization yourself then you can set
serializeFields
to false
in the options object, in this case the SDK will treat the fields object as if it is already serialized.The Freshcom API only return response body in JSON API format, however this format may not be the most convenient format to use for your client side application. This is why by default response returned from the JavaScript SDK will automatically deserialize the data. The SDK will only serialize the data and errors section of the response, meta and other sections if any will not be deserialized.
Below is an example of JSON response from the API containing a data section and its corresponding deserialized response.
API Response
Deserialized Response
{
"meta": {
"locale": "en"
},
"data": {
"id": "74b901b2-32f9-4e3b-8d4d-4eca2360550c",
"type": "Product",
"attributes": {
"kind": "simple",
"name": "Red Starship"
},
"relationships": {
"goods": {
"data": {
"id": "9fc8ee53-906f-48bd-a392-8b0709301699",
"type": "Stockable"
}
}
}
}
}
{
meta: {
locale: 'en'
},
data: {
id: '74b901b2-32f9-4e3b-8d4d-4eca2360550c',
type: 'Product',
kind: 'simple',
name: 'Red Starship',
goods: {
id: '9fc8ee53-906f-48bd-a392-8b0709301699',
type: 'Stockable'
}
}
}
Below is an example of JSON response from the API containing a errors section and its corresponding deserialized response.
API Response
Deserialized Response
{
"errors": [
{
"code": "required",
"source": {
"pointer": "/data/attributes/name"
},
"title": "Name is required"
}
]
}
{
errors: {
name: [{code: "required", title: "Name is required"}]
}
}
By default, all SDK methods returns response object will automatically deserialize the appropriate sections of the response. If you do not want the SDK to deserialize the data for you and want to handle the deserialization yourself, then you can set
deserializeResponseData
or deserializeResponseErrors
to false
in the options object. If you just want to keep to the raw response together with the deserialize response then you can set keepRawResponse
to true
, and the raw response will be included in the deserialized response and you can access it using response.raw
.Most SDK methods accepts an optional options object with the following property:
serializeFields
Boolean
(default:true
) - If the methods as a fields object indicate whether to serialize the fields or not.deserializeResponseData
Boolean
(default:true
) - If the method returns a response or aPromise
that resolves to a response indicate whether to deserialize the response data or not.deserializeResponseErrors
Boolean
(default:true
) - If the method returns a response or aPromise
that resolves to a response indicate whether to deserialize the response errors or not.keepRawResponse
Boolean
(default:false
) - If the method returns a deserialized response or aPromise
that resolves to a deserialized response indicate whether to keep the raw response.
freshcom.createAccessToken(payload)
Create a access token using the provided username and password, or refresh token.
Arguments
Returns
payload
Object
- The payload, see below for details.payload.username
String
- The username to use.payload.password
String
- The password to use.payload.refreshToken
String
- The refresh token to use.payload.grantType
String
- Can be eitherpassword
orrefresh_token
.payload.otp
String
- The one time password to use if using two factor authentication.
Returns a
Promise
that resolved to a Response
object if successful.freshcom.setAccessToken(accessToken
Sets the access token to use for all subsequent request.
Arguments
Returns
Usage
accessToken
String
- The access token to set.
Returns
undefined
.import freshcom from 'freshcom-sdk'
freshcom.setAccessToken('{access_token}')
freshcom.setRefreshToken(refreshToken)
Sets the refresh token to use when attempting to refresh the access token. If a refresh token is set then whenever a 401 is received from the API, the SDK will try to refresh the access token and and re-attempt the request. This will only happen once, if the re-attempt failed or if refreshing the access token failed then it will not be retried again.
Arguments
Returns
Usage
refreshToken
String
- The refresh token to set
Returns
undefined
.import freshcom from 'freshcom-sdk'
freshcom.setAccessToken('prt-test-827ae785-1502-4489-8a97-609c4840168')
freshcom.setDefaultLocal(defaultLocale)
Sets the default locale to use for all subsequent request. Locale can still be overwritten using the params arguments for specific methods.
Arguments
Returns
Usage
defaultLocale
String
- The default locale to use.
Returns
undefined
.import freshcom from 'freshcom-sdk'
freshcom.setDefaultLocale('zh-CN')
freshcom.retrieveAccount([params, options])
freshcom.updateAccount(fields, [params, options])
freshcom.retrieveCurrentUser([params, options])
freshcom.updateCurrentUser(fields, [params, options])
freshcom.retrieveUser(identifiers, [params, options])
freshcom.updateUser(identifiers, fields, [params, options])
freshcom.createEmailVerificationToken(fields, [params, options])
freshcom.createEmailVerification(fields, [params, options])
freshcom.createPhoneVerificationCode(fields, [params, options])
freshcom.createPasswordResetToken(fields, [params, options])
freshcom.updatePassword(identifiers, fields, [params, options])
freshcom.listOrder([params, options])
freshcom.createOrder([fields, params, options])
freshcom.retrieveOrder(identifiers, [params, options])
freshcom.updateOrder(identifiers, fields, [params, options])
freshcom.deleteOrder(identifiers, [params, options])
frescom.listOrderLineItem([params, options])
freshcom.createOrderLineItem(fields, [params, options])
freshcom.updateOrderLineItem(identifiers, fields, [params, options])
freshcom.deleteOrderLineItem(identifiers, [params, options])
freshcom.listProduct([params, options])
freshcom.createProduct(fields, [params, options])
freshcom.retrieveProduct(identifiers, [params, options])
freshcom.updateProduct(identifiers, fields, [params, options])
freshcom.deleteProduct(identifiers, [params, options])
freshcom.listPrice(identifiers, [params, options])
freshcom.createPrice(fields, [params, options])
freshcom.retrievePrice(identifiers, [params, options])
freshcom.updatePrice(identifiers, fields, [params, options])
freshcom.deletePrice(identifiers, [params, options])
freshcom.listProductCollection([params, options])
freshcom.createProductCollection(fields, [params, options])
freshcom.retrieveProductCollection(identifiers, [params, options])
freshcom.updateProductCollection(identifiers, fields, [params, options])
freshcom.deleteProductCollection(identifiers, [params, options])
freshcom.listProductCollectionMembership(identifiers, [params, options])
freshcom.createProductCollectionMembership(fields, [params, options])
freshcom.updateProductCollectionMembership(identifiers, fields, [params, options])
freshcom.deleteProductCollectionMembership(identifiers, [params, options])
freshcom.listCustomer([params, options])
freshcom.createCustomer(fields, [params, options])
freshcom.retrieveCustomer(identifiers, [params, options])
freshcom.retrieveCurrentCustomer([params, options])
freshcom.updateCustomer(identifiers, fields, [params, options])
freshcom.deleteCustomer(identifiers, [params, options])
freshcom.listPointTransaction(identifiers, [params, options])
freshcom.createPointTransaction(fields, [params, options])
freshcom.deletePointTransaction(identifiers, [params, options])
freshcom.listCard([params, options])
freshcom.updateCard(identifiers, [params, options])
freshcom.deleteCard(identifiers, [params, options])
freshcom.listPayment([params, options])
freshcom.createPayment(fields, [params, options])
freshcom.retrievePayment(identifiers, [params, options])
freshcom.updatePayment(identifiers, fields, [params, options])
freshcom.deletePayment(identifiers, [params, options])
freshcom.listRefund([params, options])
freshcom.createRefund(identifiers, fields, [params, options])
freshcom.listFulfillmentPackage([params, options])
freshcom.deleteFulfillmentPackage(identifiers, [params, options])
freshcom.updateFulfillmentItem(identifiers, fields, [params, options])
freshcom.listReturnPackage([params, options])
freshcom.createReturnItem(fields, [params, options])
freshcom.listStockable([params, options])
freshcom.createStockable(fields, [params, options])
freshcom.retrieveStockable(identifiers, [params, options])
freshcom.updateStockable(identifiers, fields, [params, options])
freshcom.deletStockable(identifiers, [params, options])
freshcom.listUnlockable([params, options])
freshcom.createUnlockable(fields, [params, options])
freshcom.retrieveUnlockable(identifiers, [params, options])
freshcom.updateUnlockable(identifiers, fields, [params, options])
freshcom.deleteUnlockable(identifiers, [params, options])
freshcom.listDepositable([params, options])
freshcom.createDepositable(fields, [params, options])
freshcom.retrieveDepositable(identifiers, [params, options])
freshcom.updateDepositable(identifiers, fields, [params, options])
freshcom.deleteDepositable(identifiers, [params, options])
freshcom.listFile([params, options])
freshcom.createFile(fields, [params, options])
freshcom.retrieveFile(identifiers, [params, options])
freshcom.updateFile(identifiers, fields, [params, options])
freshcom.uploadFile(fields, [params, options])
freshcom.deleteFile(identifiers, [params, options])
freshcom.listFileCollection([params, options])
freshcom.createFileCollection(fields, [params, options])
freshcom.retrieveFileCollection(identifiers, [params, options])
freshcom.updateFileCollection(identifiers, fields, [params, options])
freshcom.deleteFileCollection(identifiers, [params, options])
freshcom.listFileCollectionMembership(identifiers, [params, options])
freshcom.updateFileCollectionMembership(identifiers, fields, [params, options])
freshcom.deleteFileCollectionMembership(identifiers, [params, options])
freshcom.listEmail([params, options])
freshcom.listEmailTemplate([params, options])
freshcom.createEmailTemplate(fields, [params, options])
freshcom.retrieveEmailTemplate(identifiers, [params, options])
freshcom.updateEmailTemplate(identifiers, fields, [params, options])
freshcom.deleteEmailTemplate(identifiers, [params, options])
freshcom.listSmsTemplate([params, options])
freshcom.createSmsTemplate(fields, [params, options])
freshcom.retrieveSmsTemplate(identifiers, [params, options])
freshcom.updateSmsTemplate(identifiers, fields, [params, options])
freshcom.deleteSmsTemplate(identifiers, [params, options])
freshcom.listNotificationTrigger([params, options])
freshcom.createNotificationTrigger(fields, [params, options])
freshcom.retrieveNotificationTrigger(identifiers, [params, options])
freshcom.deleteNotificationTrigger(identifiers, [params, options])
Last modified 4yr ago