Options
All
  • Public
  • Public/Protected
  • All
Menu

Smartface Service-Call helper module

deprecated

USE AXIOS OR XMLHTTPREQUEST INSTEAD!

copyright

Smartface 2021

Index

Properties

Static Readonly BASE_HEADERS

BASE_HEADERS: Readonly<{ Content-Type: string; Accept: string; Accept-Language: any; Cache-Control: string }> = ...

Default values of headers

static
readonly
property

{object} header object

Protected _http

_http: IHttp

Methods

Static bodyParser

  • bodyParser(requestUrl: string, response: any): void

Static convertObjectToFormData

  • convertObjectToFormData(body: {}): string

Static getContentType

  • getContentType(headers?: Record<string, any>): any

setHeader

  • setHeader(key: string | Record<string, any>, value?: string): void
  • Sets headers for this configuration. Either sets one by each call or sets them in bulk

    method
    example
    //After login
    sc.setHeader("Authorization", "Basic 12345");
    example
    //After logout
    sc.setHeader("Authorization");
    example
    // set multiple headers at once
    sc.setHeader({
    environment: "test",
    apiVersion: "1.2" //replaces the existing
    });

    Parameters

    • key: string | Record<string, any>

      Header name to set

    • Optional value: string

      Value to set of the key. If value is not a string, key is removed from header

    Returns void

getHeaders

  • getHeaders(): Record<string, any>

createRequestOptions

  • createRequestOptions(endpointPath: string, options: IRequestOptions): IRequestOptions
  • creates a request options object for http request

    method
    example
    const reqOps = sc.createRequestOptions(`/auth/login`, {
    method: "POST",
    body: {
    userName,
    password
    },
    headers: {
    "Content-Type": "application/json"
    }
    });
    sc.request(reqOps).then((result) => {
    //logic
    }).catch((err) => {
    //logic
    });

    Parameters

    • endpointPath: string
    • options: IRequestOptions

    Returns IRequestOptions

request

  • request(endpointPath: string, options: IRequestOptions): Promise<any>
  • Performs a service call and returns a promise to handle

    method
    example
    const reqOps = sc.createRequestOptions(`/auth/login`, {
    method: "POST",
    body: {
    userName,
    password
    },
    headers: {
    "Content-Type": "application/json"
    }
    });
    sc.request(reqOps).then((result) => {
    //logic
    }).catch((err) => {
    //logic
    });

    Parameters

    • endpointPath: string
    • options: IRequestOptions

    Returns Promise<any>

Constructors

constructor

  • new default(options: IServiceCallParameters): default
  • Creates a ServiceCall helper class with common configuration to be used across multiple service calls.

    example
    // services/serviceConfig.ts
    import ServiceCall from '@smartface/extension-utils/lib/service-call';
    export const sc = new ServiceCall({
    baseUrl: "http://api.myBaseUrl.com",
    logEnabled: true,
    headers: {
    apiVersion: "1.0"
    }
    });

    // services/user.ts
    import { sc } from 'services/serviceConfig"';

    function login(userName, password) {
    return new Promise((resolve, reject) => {
    sc.request(`/auth/login?emine=3`, {
    method: "POST",
    body: {
    userName,
    password
    }
    }).then(response => {
    sc.setHeader("Authorization", "Bearer " + response.token);
    resolve(response);
    }).catch(err => {
    reject(err);
    });
    });
    }


    // pages/pgLogin.ts
    import userService from 'services/user';

    this.btnLogin.onPress = () => {
    userService.login(this.tbUserName.text, this.tbPassword.text).then(()=> {
    this.router.push("dashboard");
    }).catch(()=> {
    alert("Cannot login");
    });
    };

    Parameters

    • options: IServiceCallParameters

      Cofiguration of service call helper object (required)

    Returns default

Accessors

baseUrl

  • get baseUrl(): string
  • set baseUrl(value: string): void
  • Base URL for this service-call library uses. This can be get and set

    property

    {string} baseUrl

    Returns string

  • Base URL for this service-call library uses. This can be get and set

    Parameters

    • value: string

    Returns void

logEnabled

  • get logEnabled(): boolean
  • set logEnabled(value: boolean): void
  • Log enabled for service-call. This can be get and set

    property

    {boolean} logEnabled

    Returns boolean

  • Log enabled for service-call. This can be get and set

    Parameters

    • value: boolean

    Returns void

Generated using TypeDoc