0.0.7 • Published 6 years ago

omnis v0.0.7

Weekly downloads
8
License
ISC
Repository
github
Last release
6 years ago

Omnis

node module for integration with Omnis Platform omnis npm omnis-cli npm


Instalation

npm install omnis 

or

yarn add omnis

Quick Start

After installtion generate omnis.json on application settings page (read guide). And put this file into root project directory.

For requiring Omnis use following code:

import Omnis from 'omnis'

Omnis.get('test_endpoint')
  .then(res => console.log(res))
  .catch(err => console.log(err))

Then run your project, if everything configuration was properly done. In dev tools, you will see the next output.

Methods

Omnis node module has methods, which allow developers to manipulate the data on the server side by make HTTP requests. You don't need to modelate any queries for sending data to the server, just use Omnis methods and paste data, which you want to send on a server, like an argument to the method.

GET

GET method used for getting data from endpoint.

import get from 'omnis/get'

let args = [
  { limit: 1 },
  { order: 'asc' },
  { page: 1 },
  { per_page: 4 },
  { q: '' },
  { slug: 'posts' }
]

get('test', ...args)
  .then(res => console.log(res))
  .catch(err => console.error(err))

Arguments

GET method takes like a first argument - endpoint name, the second argument is the array of arguments:

NameTypeAllow valueDefaultDetails
limitintigerwhole numbersnullReturns first items in the limit
orderstring'desc', 'asc''desc'Returns ordered by date items. asc - ascending order, desc - descending order
pageintigerwhole numbersnullReturns items per page
per_pageintigerwhole numbersnullReturns items on the page
qstringallnullReturns items with matching data
idsnumberwhole numbersnullReturns item with matching id
slugstringallnullReturns item with matching slug

POST

POST method used for send and write data on endpoint.

import post from 'omnis/post'

const data = {
  title: 'Lorem',
  subtitle: 'Ipsum'
}

post('test', data)
  .then(res => console.log(res))
  .catch(err => console.log(err))

Arguments

POST method takes like first argument - endpoint name, the second argument is the data object with any object keys and a values.

NameTypeAllow valueDefault
dataobjectallnull

PUT

PUT method used for replacing all data in endpoint.

import put from 'omnis/put'

const data = {
  title: 'Lorem',
  subtitle: 'Ipsum'
}

put('test', data)
  .then(res => console.log(res))
  .catch(err => console.log(err))

Arguments

PUT method takes like the first argument - endpoint name, the second argument is the data object with any object keys and values, on which will be replaced all endpoint data.

NameTypeAllow valueDefault
dataobjectallnull

PATCH

PATCH method used for update existing data in endpoint.

import patch from 'omnis/patch'

const data = {
  title: 'Lorem',
  subtitle: 'Ipsum'
}

patch('test', data, id)
  .then(res => console.log(res))
  .catch(err => console.log(err))

Arguments

PATCH method takes like the first argument - endpoint name, the second argument is the data object with any object keys and values. The third argument is id of the item which you want to update.

NameTypeAllow valueDefault
dataobjectstringnull
idsarrayallnull

DELETE

DELETE method used for deleting existing data from endpoint.

import delete from 'omnis/remove' //package has a name remove because delete is reserved the name in JavaScript

const ids = ['first_id', 'second_id']

delete('test', ids)
  .then(res => console.log(res))
  .catch(err => console.log(err))

Arguments

DELETE method takes like first argument - endpoint name, the second argument is the ids array of items which you want to delete from the endpoint. If you don't pass any ids all items from endpoint will be deleted.

NameTypeAllow valueDefaultDetails
idsarraystringnullReturn true if there no errors

File

File method used for upload file or image (image endpoint type) to endpoint.

import file from 'omnis/file'

const file = document.querySelector('input[type="file"]').files[0]

file('test', file)
  .then(res => console.log(res))
  .catch(err => console.log(err))

Arguments

File method takes like first argument - endpoint name, the second argument is the file object. Max file size 10mb.

NameTypeAllow valueDefaultDetails
fileobjectfile objectnullmax file size 10mb

Email

Email method used for sending email to user from omnis and write them on the endpoint.

import email from 'omnis/email'

const email = {
  email_to: 'test@example.com',
  email_subject: 'test subject',
  content: 'Hello World!'
}

email('test', email)
  .then(res => console.log(res))
  .catch(err => console.log(err))

Arguments

Email method takes like first argument - endpoint name, the second argument is the email object.

NameTypeAllow valueDefaultDetails
email_tostringemailnull
email_subjectstringallnull
contenttext/htmlallnull

Connect

Connect method used for listening updates in endpoint in real-time.

import connect from 'omnis/connect'

connect('test')
  .onmessage = res => console.log(res)

Arguments

Email method takes like first argument - endpoint name.

User

Omnis Platform can serve users, for it you can use following methods

Create

Create methods used for creating user

import create from 'omnis/user/create'

const image = document.querySelector('input[type="file"]').files[0]

const user = {
  username: 'John Smith',
  email: 'test@example.com',
  password: '1234567890',
  passwordConfirmation: '1234567890',
  image: image,
  content: {
    phoneNumber: '(123) 456-7890',
    country: 'United Kingdom'
  }
}

create('test', user)
  .then(res => console.log(res))
  .catch(err => console.log(err))

Arguments

Create method takes like first argument - endpoint name, the second argument is the user object. In user object username, email, password, passwordConfirmation are required.

NameTypeAllow valueDefaultDetails
usernamestringallnull
emailstringemailnull
passwordstringallnullmatch to passwordConfirmation
passwordConfirmationstringallnullmatch to password
imageobjectfile objectnullmax file size 10mb
contentobjectallnull

Show

Show method used for returning user by userId

import show from 'omnis/user/show'

show('test', 'userId')
  .then(res => console.log(res))
  .catch(err => console.log(err))

Arguments

Show method takes like first argument - endpoint name, the second argument is the userId.

NameTypeAllow valueDefaultDetails
userIdstringallnull

Edit

Edit method used for edit current user and return updated user object

import edit from 'omnis/user/edit'

const updatedUser = {
  username: 'NewUserName',
  email: 'newemail@example.com',
  password: 'NewPassword',
  passwordConfirmation: 'NewPassword',
  content: {
    ... new content
  }
}

edit('test', updatedUser, userId, sessionToken)
  .then(res => console.log(res))
  .catch(err => console.log(err))

Arguments

Edit method takes like first argument - endpoint name, the second argument is the user object. In user object username, email, password, passwordConfirmation are required.

NameTypeAllow valueDefaultDetails
userIdstringnumbernull
sessionTokenstringnull
usernamestringallnull
emailstringemailnull
passwordstringallnullmatch to passwordConfirmation
passwordConfirmationstringallnullmatch to password
imageobjectfile objectnullmax file size 10mb
contentobjectallnull

Login

Login method used for login the user and returns a current user object

import login from 'omnis/user/login'

const auth = {
  email: 'test@example.com'
  password: '1234567890'
}

login('test', auth)
  .then(res => console.log(res))
  .catch(err => console.log(err))

Arguments

Login method takes like first argument - endpoint name, the second argument is the auth object.

NameTypeAllow valueDefaultDetails
emailstringemailnull
passwordstringallnull

Logout

Logout method used for logout the user

import logout from 'omnis/user/logout'

logout('test', userId, sessionToken)
  .then(res => console.log(res))
  .catch(err => console.log(err))

Arguments

Logout method takes like first argument - endpoint name, the second argument is the userId, the third argument is sessionToken from the current user object.

NameTypeAllow valueDefaultDetails
userIdstringnumbernull
sessionTokenstringnull

Password Reset

Password reset method used for resetting user password and returning user_token. Which will be used in the next method.

import resetPassword from 'omnis/user/resetPassword'

resetPassword('test', email)
  .then(res => console.log(res))
  .catch(err => console.log(err))

Arguments

Password reset method takes like first argument - endpoint name, the second argument is the user email address.

NameTypeAllow valueDefaultDetails
emailstringemailnull

Password Update

Password update method used for update user password dropped in the Password Reset method and returning updated user object.

import passwordUpdate from 'omnis/user/passwordUpdate'

const newPassword = {
  password: '1234567890',
  passwordConfirmation: '1234567890'
}

passwordUpdate('test', newPassword, userToken)
  .then(res => console.log(res))
  .catch(err => console.log(err))

Arguments

Password update method takes like first argument - endpoint name, the second argument is the new password object, the third argument is userToken from the Password Reset method.

NameTypeAllow valueDefaultDetails
passwordstringallnullmatch to passwordConfirmation
passwordConfirmationstringallnullmatch to password
userTokenstringnull

Remove

Remove method used for deleting current user.

import remove from 'omnis/user/remove'

remove('test', userId, sessionToken)
  .then(res => console.log(res))
  .catch(err => console.log(err))

Arguments

Remove method takes like first argument - endpoint name, the second argument is userId, the third argument is sessionToken.

NameTypeAllow valueDefaultDetails
userIdstringnumbernull
sessionTokenstringnull