Home Reference Source

Function

Static Public Summary
public

afterDelete(recordType: string, func: function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *, options: Object)

afterDelete — executes decorated function after a record delete operation occurs

public

afterSave(recordType: string, func: function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *, options: Object)

afterSave — executes decorated function after a record save operation occurs

public

beforeDelete(recordType: string, func: function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *, options: Object)

beforeDelete — executes decorated function before a record delete operation occurs

public

beforeSave(recordType: string, func: function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *, options: Object)

beforeSave — executes decorated function before a record save operation occurs

public

async configModule(moduleName: *, options: *)

Import and config the cloud code plugin module.

public

createLogger(name: *, context: {}, opts: {}): *

public

databaseConfigFromURL(databaseURL: *): *

public

errorResponseFromMessage(message: *): *

public

event(name: String, func: function(param: *), options: Object)

You can register the cloud code to run at skygear life cycle event.

public

every(cron: String, func: function(param: *), options: Object)

You can register the cloud code to get run at specific time intervals like a cron job.

public

fromJSON(attrs: Object): Object

Convert object from JSON representation

public

getContainer(userId: String): CloudCodeContainer

Get a configured CloudCodeContainer.

public

Get redirect link result, return user from redirect based login flow if link success, promise resolve with result { result: 'OK' }.

public

Get redirect login result, return user from redirect based login flow if login success, promise resolve with logged in user.

public

async getOAuthProviderProfiles(): Promise

Get current user's provider profiles, can use for determine user logged in provider

public

getRoleNameFromParams(roleOrRoleName: Role | String): String

public

Return a shared signer for the current configuration.

public

getUserIDFromParams(userOrUserID: Record | String): String

Get user ID from function parameter.

public

handler(path: string, func: function(request: *, options: *): object, options: Object)

You can configure the cloud code as an HTTP handler, which can respond to requests coming from outside the SDK.

public

hook(name: String, func: function(newRecord: *, oldRecord: *, pool: *, options: *), options: Object)

You can register the cloud code to run at database event.

public

async iframeHandler(): Promise

Iframe handler script.

public

isAsset(obj: *): Boolean

Returns whether an object is a Skygear Asset.

public

isLocalStorageValid(): boolean

public

isRecord(obj: *): Boolean

Returns whether an object is a Skygear Record.

public

isRole(obj: *): *

public

isValueType(value: *): *

public

async linkOAuthProviderWithAccessToken(provider: String, accessToken: String): Promise

Link oauth provider with access token

public

async linkOAuthProviderWithPopup(provider: String, options: Object): Promise

Link oauth provider with popup window

public

linkOAuthProviderWithRedirect(provider: String, options: Object): Promise

Link oauth provider with redirect

public

async loginOAuthProviderWithAccessToken(provider: String, accessToken: String): Promise

Login oauth provider with access token

public

async loginOAuthProviderWithPopup(provider: String, options: Object): Promise

Login oauth provider with popup window

public

loginOAuthProviderWithRedirect(provider: String, options: Object): Promise

Login oauth provider with redirect

public

async loginWithCustomToken(token: String): Promise

Login with custom token.

public

async oauthHandler(): Promise

Auth flow handler script

public

op(name: String, func: function(param: Object, options: *), options: Object)

You can register the cloud code as lambda, with JSON input and output.

public

parseInteger(val: *, defaultValue: *): *

public

poolConnect(callback: function(err: *, client: *, done: function): *)

Connect to a pg connection provided by pg pool.

public

provides(providerType: string, providerID: string, ProviderCls: object, options: object)

Authentication Provider allows a plugin to authenticate user based on credentials from a third-party.

public

async publishEventsToChannels(channels: String[], eventsData: Object[]): Promise<CloudCodeContainer>

Publish multiple events to multiple channels.

public

staticAsset(mountPoint: string, func: function(): string)

staticAsset — declare certain path to be serve in static asset.

public

staticAssetHandler(req: *): *

staticAssetHandler — default handler for serving static assets with during development.

public

toJSON(v: Object): Object

Returns the specified value to a JSON representation.

public

async unlinkOAuthProvider(provider: String): Promise

Unlink oauth provider

Static Public

public afterDelete(recordType: string, func: function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *, options: Object) source

afterDelete — executes decorated function after a record delete operation occurs

Params:

NameTypeAttributeDescription
recordType string

The type of the record.

func function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *

function to be registered.

options Object
  • optional
options.async Boolean
  • optional

true if the function triggered

Example:

const skygearCloud = require('skygear/cloud');¬
skygearCloud.afterDelete('note', function(record, original, pool, options) {
    // cloud code handling the request
    return;
}, {
    async: true
});

public afterSave(recordType: string, func: function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *, options: Object) source

afterSave — executes decorated function after a record save operation occurs

Params:

NameTypeAttributeDescription
recordType string

The type of the record.

func function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *

function to be registered.

options Object
  • optional
options.async Boolean
  • optional

true if the function triggered

Example:

const skygearCloud = require('skygear/cloud');¬
skygearCloud.afterSave('note', function(record, original, pool, options) {
    // cloud code handling the request
    return;
}, {
    async: true
});

public beforeDelete(recordType: string, func: function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *, options: Object) source

beforeDelete — executes decorated function before a record delete operation occurs

Params:

NameTypeAttributeDescription
recordType string

The type of the record.

func function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *

function to be registered.

options Object
  • optional
options.async Boolean
  • optional

true if the function triggered

Example:

const skygearCloud = require('skygear/cloud');¬
skygearCloud.beforeDelete('note', function(record, original, pool, options) {
    // cloud code handling the request
    return;
}, {
    async: true
});

public beforeSave(recordType: string, func: function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *, options: Object) source

beforeSave — executes decorated function before a record save operation occurs

Params:

NameTypeAttributeDescription
recordType string

The type of the record.

func function(record: lib/record.js~Record, originalRecord: lib/record.js~Record, pool: pool, options: *): *

function to be registered.

options Object
  • optional
options.async Boolean
  • optional

true if the function triggered asynchronously

Example:

const skygearCloud = require('skygear/cloud');¬
skygearCloud.beforeSave('note', function(record, original, pool, options) {
    // cloud code handling the request
    return;
}, {
    async: true
});

public async configModule(moduleName: *, options: *) source

Import and config the cloud code plugin module.

This function will load the specified module by name using the require function. The specified module must exists or an error is thrown.

It is expected that the module will exports a function called includeme. This function will also call the includeme function to config the module.

Params:

NameTypeAttributeDescription
moduleName *
options *

public createLogger(name: *, context: {}, opts: {}): * source

Params:

NameTypeAttributeDescription
name *
context {}
  • optional
  • default: {}
opts {}
  • optional
  • default: {}

Return:

*

public databaseConfigFromURL(databaseURL: *): * source

import {databaseConfigFromURL} from 'skygear-build/packages/skygear-core/lib/cloud/pgurl.js'

Params:

NameTypeAttributeDescription
databaseURL *

Return:

*

public errorResponseFromMessage(message: *): * source

import {errorResponseFromMessage} from 'skygear-build/packages/skygear-sso/lib/util.js'

Params:

NameTypeAttributeDescription
message *

Return:

*

public event(name: String, func: function(param: *), options: Object) source

You can register the cloud code to run at skygear life cycle event.

Params:

NameTypeAttributeDescription
name String

skygear life cycle event name

func function(param: *)

function to be registered

options Object
  • optional

Example:

const skygearCloud = require('skygear/cloud');
skygearCloud.event('after-plugins-ready', function() {
  console.log('Meow');
});

public every(cron: String, func: function(param: *), options: Object) source

You can register the cloud code to get run at specific time intervals like a cron job.

Params:

NameTypeAttributeDescription
cron String

time interval in cron job syntax

func function(param: *)

function to be registered

options Object
  • optional

Example:

const skygearCloud = require('skygear/cloud');
skygearCloud.every('@daily', function() {
  console.log('Meow');
});

public fromJSON(attrs: Object): Object source

Convert object from JSON representation

It will descends into array and object to convert Skygear Data Type from JSON representation. If the specified value is null, null is returned.

This function is the opposite of toJSON.

Params:

NameTypeAttributeDescription
attrs Object

the object or value in JSON representation

Return:

Object

the result in Skygear Data Type

public getContainer(userId: String): CloudCodeContainer source

Get a configured CloudCodeContainer.

Params:

NameTypeAttributeDescription
userId String
  • optional

user ID of the user. If not specified, the default is determined by the server.

Return:

CloudCodeContainer

the cloud code container that acts as the specified user.

public getLinkRedirectResult(): Promise source

import {getLinkRedirectResult} from 'skygear-build/packages/skygear-sso/lib/oauth.js'

Get redirect link result, return user from redirect based login flow if link success, promise resolve with result { result: 'OK' }. if link fail, promise fail with error. if no redirect flow was called, promise resolve with empty result.

Return:

Promise

promise

Example:

skygear.auth.getLinkRedirectResult().then(...);

public getLoginRedirectResult(): Promise source

import {getLoginRedirectResult} from 'skygear-build/packages/skygear-sso/lib/oauth.js'

Get redirect login result, return user from redirect based login flow if login success, promise resolve with logged in user. if login fail, promise fail with error. if no redirect flow was called, promise resolve with empty result.

Return:

Promise

promise

Example:

skygear.auth.getLoginRedirectResult().then(...);

public async getOAuthProviderProfiles(): Promise source

import {getOAuthProviderProfiles} from 'skygear-build/packages/skygear-sso/lib/oauth.js'

Get current user's provider profiles, can use for determine user logged in provider

Return:

Promise

promise

Example:

skygear.auth.getOAuthProviderProfiles().then(...);

public getRoleNameFromParams(roleOrRoleName: Role | String): String source

import {getRoleNameFromParams} from 'skygear-build/packages/skygear-core/lib/util.js'

Params:

NameTypeAttributeDescription
roleOrRoleName Role | String

a role or a role name

Return:

String

the name of the role

public getSigner(): Signer source

Return a shared signer for the current configuration.

Return:

Signer

public getUserIDFromParams(userOrUserID: Record | String): String source

import {getUserIDFromParams} from 'skygear-build/packages/skygear-core/lib/util.js'

Get user ID from function parameter.

Params:

NameTypeAttributeDescription
userOrUserID Record | String

a user record or a user ID

Return:

String

the ID of the user

public handler(path: string, func: function(request: *, options: *): object, options: Object) source

You can configure the cloud code as an HTTP handler, which can respond to requests coming from outside the SDK. A custom HTTP endpoint can be created using the handler function.

A custom HTTP endpoint can be useful for the followings:

  • receiving requests from outside the Skygear SDK
  • allowing a third party webhook to call upon (e.g. payment service)

Params:

NameTypeAttributeDescription
path string

The path of the handler to be mount.

func function(request: *, options: *): object

function to be registered.

options Object
  • optional
options.method String[] | String
  • optional

handler methods, e.g. GET, POST

options.keyRequired Boolean
  • optional

require api key to call the lambda

options.userRequired Boolean
  • optional

require user to call the lambda

Example:

const skygearCloud = require('skygear/cloud');¬
skygearCloud.handler('handler1', function(req) {
    // cloud code handling the request
    return 'String';
}, {
    method: ['GET', 'POST'],
    userRequired: false
});
skygearCloud.handler('private', function(req, options) {
    // cloud code handling the request
    const {
      context,
      container  // a cloud code container for the current request context
    } = options;
    return {
      status: 'ok',
      user_id: context.user_id // only available if userRequired=true
    };
}, {
    method: ['GET', 'POST'],
    userRequired: true
});

public hook(name: String, func: function(newRecord: *, oldRecord: *, pool: *, options: *), options: Object) source

You can register the cloud code to run at database event.

Params:

NameTypeAttributeDescription
name String

function name

func function(newRecord: *, oldRecord: *, pool: *, options: *)

function to be registered

options Object
  • optional
options.type String
  • optional

record type

options.trigger String
  • optional

type of database event that trigger the hook

options.async Boolean
  • optional

true if the function triggered asynchronously

Example:

const skygearCloud = require('skygear/cloud');
skygearCloud.hook('before-save', function(newRecord, oldRecord, pool) {
  console.log('Meow');
  return newRecord;
});

public async iframeHandler(): Promise source

Iframe handler script. When getLoginRedirectResult is called, sdk will inject an iframe in the document with plugin iframe handler endpoint the endpoint will call this handler. Handler will get the sso result from browser session and post the result back to parnet

Return:

Promise

promise

Example:

skygear.auth.iframeHandler().then(...);

public isAsset(obj: *): Boolean source

Returns whether an object is a Skygear Asset.

Params:

NameTypeAttributeDescription
obj *

Return:

Boolean

true if the specified object is a Skygear Asset.

public isLocalStorageValid(): boolean source

import {isLocalStorageValid} from 'skygear-build/packages/skygear-core/lib/util.js'

Return:

boolean

public isRecord(obj: *): Boolean source

Returns whether an object is a Skygear Record.

Params:

NameTypeAttributeDescription
obj *

Return:

Boolean

true if the specified object is a Skygear Record.

public isRole(obj: *): * source

Params:

NameTypeAttributeDescription
obj *

Return:

*

public isValueType(value: *): * source

Params:

NameTypeAttributeDescription
value *

Return:

*

public async linkOAuthProviderWithAccessToken(provider: String, accessToken: String): Promise source

import {linkOAuthProviderWithAccessToken} from 'skygear-build/packages/skygear-sso/lib/oauth.js'

Link oauth provider with access token

Params:

NameTypeAttributeDescription
provider String

name of provider, e.g. google, facebook

accessToken String

provider app access token

Return:

Promise

promise

Example:

skygear.auth.linkOAuthProviderWithAccessToken(provider, accessToken).then(...);

public async linkOAuthProviderWithPopup(provider: String, options: Object): Promise source

import {linkOAuthProviderWithPopup} from 'skygear-build/packages/skygear-sso/lib/oauth.js'

Link oauth provider with popup window

Params:

NameTypeAttributeDescription
provider String

name of provider, e.g. google, facebook

options Object

options for generating auth_url

options.callbackURL String

target url for the popup window to post the result message

options.scope Array<String>

oauth scopes params

options.options Object

add extra query params to provider auth url

Return:

Promise

promise

Example:

skygear.auth.linkOAuthProviderWithPopup('google').then(...);

public linkOAuthProviderWithRedirect(provider: String, options: Object): Promise source

import {linkOAuthProviderWithRedirect} from 'skygear-build/packages/skygear-sso/lib/oauth.js'

Link oauth provider with redirect

Params:

NameTypeAttributeDescription
provider String

name of provider, e.g. google, facebook

options Object

options for generating auth_url

options.callbackURL String

target url for the popup window to post the result message

options.scope Array<String>

oauth scopes params

options.options Object

add extra query params to provider auth url

Return:

Promise

promise

Example:

skygear.auth.linkOAuthProviderWithRedirect('google').then(...);

public async loginOAuthProviderWithAccessToken(provider: String, accessToken: String): Promise source

import {loginOAuthProviderWithAccessToken} from 'skygear-build/packages/skygear-sso/lib/oauth.js'

Login oauth provider with access token

Params:

NameTypeAttributeDescription
provider String

name of provider, e.g. google, facebook

accessToken String

provider app access token

Return:

Promise

promise

Example:

skygear.auth.loginOAuthProviderWithAccessToken(provider, accessToken).then(...);

public async loginOAuthProviderWithPopup(provider: String, options: Object): Promise source

import {loginOAuthProviderWithPopup} from 'skygear-build/packages/skygear-sso/lib/oauth.js'

Login oauth provider with popup window

Params:

NameTypeAttributeDescription
provider String

name of provider, e.g. google, facebook

options Object

options for generating auth_url

options.callbackURL String

target url for the popup window to post the result message

options.scope Array<String>

oauth scopes params

options.options Object

add extra query params to provider auth url

Return:

Promise

promise

Example:

skygear.auth.loginOAuthProviderWithPopup('google').then(...);

public loginOAuthProviderWithRedirect(provider: String, options: Object): Promise source

import {loginOAuthProviderWithRedirect} from 'skygear-build/packages/skygear-sso/lib/oauth.js'

Login oauth provider with redirect

Params:

NameTypeAttributeDescription
provider String

name of provider, e.g. google, facebook

options Object

options for generating auth_url

options.callbackURL String

target url for the popup window to post the result message

options.scope Array<String>

oauth scopes params

options.options Object

add extra query params to provider auth url

Return:

Promise

promise

Example:

skygear.auth.loginOAuthProviderWithRedirect('google').then(...);

public async loginWithCustomToken(token: String): Promise source

import {loginWithCustomToken} from 'skygear-build/packages/skygear-sso/lib/custom_token.js'

Login with custom token.

The custom token is typically created on an external server hosting a user database. This server generates the custom token so that the user on an external server can log in to Skygear Server.

Params:

NameTypeAttributeDescription
token String

the token string

Return:

Promise

promise

Example:

skygear.auth.loginWithCustomToken('eyXXXXXXXX').then(...);

public async oauthHandler(): Promise source

Auth flow handler script

Return:

Promise

promise

Example:

skygear.auth.oauthHandler().then(...);

public op(name: String, func: function(param: Object, options: *), options: Object) source

You can register the cloud code as lambda, with JSON input and output.

This is a convenient way to inject custom logic to Skygear server and can be called easily with skygear.lambda in the client side.

Params:

NameTypeAttributeDescription
name String

lambda name

func function(param: Object, options: *)

function to be registered

options Object
  • optional
options.keyRequired Boolean
  • optional

require api key to call the lambda

options.userRequired Boolean
  • optional

require user to call the lambda

Example:

const skygearCloud = require('skygear/cloud');
skygearCloud.op('greeting', function(param) {
    return {
      'content': 'Hello, ' + param.name,
    };
}, {
    userRequired: false
});

public parseInteger(val: *, defaultValue: *): * source

Params:

NameTypeAttributeDescription
val *
defaultValue *

Return:

*

public poolConnect(callback: function(err: *, client: *, done: function): *) source

Connect to a pg connection provided by pg pool.

The pg connection is automatically set to use the application's database schema.

Params:

NameTypeAttributeDescription
callback function(err: *, client: *, done: function): *

function called after setting up the database connection

Example:

const skygearCloud = require('skygear/cloud');
skygearCloud.poolConnect(function (err, client, done) {
  // if err is undefined or null, make query using the client
  client.query(
    'SELECT id FROM _user WHERE username = $1',
    ['johndoe'],
    function(queryErr, result) {
      done();
      // use the query result here
    }
  );
}

public provides(providerType: string, providerID: string, ProviderCls: object, options: object) source

Authentication Provider allows a plugin to authenticate user based on credentials from a third-party. The Authentication Provider responds to Skygear Server whether the credentials are accepted.

Params:

NameTypeAttributeDescription
providerType string

Type of the provider, only auth is supported now.

providerID string

unique identifier to the provider

ProviderCls object

a provider class

options object
  • optional

options for setting

Example:

const skygearCloud = require('skygear/cloud');¬

skygearCloud.provides('auth', 'com.facebook', Provider);

public async publishEventsToChannels(channels: String[], eventsData: Object[]): Promise<CloudCodeContainer> source

import {publishEventsToChannels} from 'skygear-build/packages/skygear-core/lib/cloud/container.js'

Publish multiple events to multiple channels.

Params:

NameTypeAttributeDescription
channels String[]
  • optional

name of the channels

eventsData Object[]
  • optional

events data to be published

Return:

Promise<CloudCodeContainer>

promise with the cloud code container

public staticAsset(mountPoint: string, func: function(): string) source

staticAsset — declare certain path to be serve in static asset.

All asset will be serve with the prefix /static. i.e. if the mount ping is declare as /css. The final URL for requesting assets will be /static/css.

Params:

NameTypeAttributeDescription
mountPoint string

the target mount point

func function(): string

function to return the absolute path of the static assets.

Example:

const skygearCloud = require('skygear/cloud');¬
skygearCloud.staticAsset('/styles', function() {
    // Return the absolute path of the static assets directory
    // http://<yourapp>.skygeario.com/static/styles will be serving files
    // located at '<project_path>/css`
    return __dirname + '/css/';
});

public staticAssetHandler(req: *): * source

staticAssetHandler — default handler for serving static assets with during development.

Params:

NameTypeAttributeDescription
req *

Return:

*

public toJSON(v: Object): Object source

Returns the specified value to a JSON representation.

It will descends into array and object to convert Skygear Data Type into JSON representation. If the specified value is null, null is returned.

The output of this function differ from Record.toJSON when specifying a Record. This function will wrap a Record in JSON representation with a $type=record object.

This function is the opposite of fromJSON.

Params:

NameTypeAttributeDescription
v Object

the object or value value to convert to JSON

Return:

Object

the result in JSON representation

public async unlinkOAuthProvider(provider: String): Promise source

import {unlinkOAuthProvider} from 'skygear-build/packages/skygear-sso/lib/oauth.js'

Unlink oauth provider

Params:

NameTypeAttributeDescription
provider String

name of provider, e.g. google, facebook

Return:

Promise

promise

Example:

skygear.auth.unlinkOAuthProvider(provider).then(...);