Get Started
OneEntry Platform SDK is an SDK that provides an easy way to interact with the OneEntry Platform API.
Official Site
Visit the official OneEntry website at https://oneentry.cloud to learn more about the OneEntry Platform.
Sign Up
To get started with OneEntry, sign up for an account at https://account.oneentry.cloud/authentication/register.
Installation
To install the OneEntry Platform SDK in your project, run the following command:
npm install oneentry
To use the OneEntry Platform SDK in your project, import the defineOneEntry function:
import { defineOneEntry } from 'oneentry'
const config = {
token: 'your-app-token',
}
const {
Admins,
AttributesSets,
AuthProvider,
Blocks,
Events,
Forms,
FormData,
FileUploading,
GeneralTypes,
IntegrationCollections,
Locales,
Menus,
Orders,
Pages,
Products,
ProductStatuses,
System,
Templates,
TemplatePreviews,
Users,
WS,
} = defineOneEntry('your-url', config)
Or
const config = {
token: 'your-app-token',
}
const api = defineOneEntry('your-url', config)
Config
The second parameter of the constructor takes the 'config'. It contains the following values:
-
'token' - Set the token key if your project requires a "Security API Token". If you are using certificate protection, do not pass this variable. You can read more about the security of your project here.
-
'langCode' - Set the "langCode" to establish the default language. By specifying this parameter once, you won't have to pass the langCode to the methods of the ONEENTRY API. If you do not specify a default language, it will default to "en_US".
-
'traficLimit' - Some methods use more than one request to the CMS to ensure that the data you receive is complete and easy to work with. Set this parameter to "true" to save traffic and decide for yourself what data you need. The default value is "false".
-
'auth' - An object with authorization settings. By default, the SDK is configured to work with tokens stored in the user's session and does not require any additional work from you. However, the SDK does not maintain session state between sessions. If you are satisfied with these settings, you do not need to pass the 'auth' variable at all.
The 'auth' object contains the following settings:
-
'refreshToken' - The user's refresh token. Pass it here from the repository to restore the user's session during initialization.
-
'saveFunction' - A function that handles the update of the refresh token. If you want to store the token between sessions, for example in local storage, provide a function here that does this. The function must accept a parameter to which the string containing the token will be passed.
-
'customAuth' - If you want to configure authorization and manage tokens yourself, set this flag to true. If you prefer to use the SDK's default settings, set it to false or omit it entirely.
An example of a configuration with token protection and automatic authentication that maintains state between sessions:
const tokenFunction = (token) => {
localStorage.setItem('refreshToken', token)
}
const api = defineOneEntry('https://my-project.oneentry.cloud', {
token: 'my-token',
langCode: 'en_US',
auth: {
refreshToken: localStorage.getItem('refreshToken'),
saveFunction: tokenFunction,
},
})
An example of a configuration that is protected with a certificate allows you to configure the authorization system yourself and saves data on requests:
const api = defineOneEntry('https://my-project.oneentry.cloud', {
langCode: 'en_US',
traficLimit: true,
auth: {
customAuth: true,
refreshToken: localStorage.getItem('refreshToken'),
},
})
If you choose to manage tokens yourself, you can pass the token to the method as follows. The intermediate method allows you to pass an access token to the request. Then call the required method. This method (setAccessToken) should not be called if the method does not require user authorization.
const user = api.Users.setAccessToken('my.access.token').getUser()
If you opted for token protection to ensure connection security, simply pass your token to the function as an optional parameter.
You can obtain a token as follows:
- Log in to your personal account.
- Go to the "Projects" tab and select a project.
- Navigate to the "Access" tab.
- Toggle the switch to "Security API Token".
- Log in to the project, go to the settings section, and open the token tab.
- Retrieve and copy your project's token.
You can also connect a TLS certificate to protect your project. In this case, do not pass the "token" at all. When using the certificate, set up a proxy in your project. Pass an empty string as the URL parameter. Learn more about security
const saveTokenFromLocalStorage = (token) => {
localStorage.setItem('refreshToken', token)
}
const api = defineOneEntry('your-url', {
token: 'my-token',
langCode: 'my-langCode',
auth: {
customAuth: false,
userToken: 'refresh.token',
saveFunction: saveTokenFromLocalStorage,
},
})
Errors
If you want to handle errors within the SDK, leave the "errors" property at its default setting. In this case, you will receive either the entity data or an error object. You need to perform a type check, for example, by checking the statusCode property with ".hasOwnProperty".
However, if you prefer to use the "try catch(e) " construct, set the "isShell" property to "false". In this case, you will need to handle the error using "try catch(e) ".
Additionally, you can pass custom functions that will be called within the SDK with the appropriate error code. These functions receive an error object as an argument, which you can process as needed.
const api = defineOneEntry('your-url', {
token: 'my-token',
langCode: 'my-langCode',
errors: {
isShell: false,
customErrors: {
400: (error) => console.error(error.message),
404: (error) => console.error(error.message),
500: (error) => console.error(error.message),
},
},
})