@superoffice/tsclient.webapi
TypeScript icon, indicating that this package has built-in type declarations

10.2.6-1262 • Public • Published

SuperOffice WebAPI TypeScript client

Provides a strongly-typed API to the SuperOffice Agents (web services).

You can either set up a WebApi connection object, and use it as a factory to get agents:

import { WebApi } from "@superoffice/tsclient.webapi";

let webapi = new WebApi("http://example.com/SuperOffice");
let agent = webapi.getWebhookAgent();
let hook = await agent.CreateDefaultWebhook();
hook.Name = "My hook";
hook.Events = ["contact.created"];
hook.TargetUrl = "https://www.requestbin.com";
let hook = await agent.SaveWebhook(hook);

or you can configure the WebApi using a static method, and let the agents re-use the global configuration:

import { WebApi, WebhookAgent } from "@superoffice/tsclient.webapi";

WebApi.configure("http://example.com/SuperOffice");
WebApi.authenticateWithPassword("username", "password");
let agent = new WebhookAgent();
let hook = await agent.CreateDefaultWebhook();

or you can configure the agents directly

import { WebhookAgent } from "@superoffice/tsclient.webapi";

let config: AxiosRequestConfig = { auth: { username: "username", password: "password" } };
let agent = new WebhookAgent("http://example.com/SuperOffice", config);
let hook = await agent.CreateDefaultWebhook();

Request parameters are also typed, so you can define objects like this:

import { WebApi, WebhookAgent } from "@superoffice/tsclient.webapi";
import { Webhook } from "@superoffice/tsclient.webapi/src/WebApi/Carriers";

let webhook: Webhook = {
  Name: "My hook",
  Events: ["contact.created"],
  Type: "webhook",
  TargetUrl: "https://www.requestbin.com"
};
WebApi.configure("http://example.com/SuperOffice");
let agent = new WebhookAgent();
let hook = await agent.SaveWebhook(webhook);

The hook object is typed, and its properties are all marked as optional, so that you don't have to fill in everything.

The agent methods return promises, so these are all thenable.

let agent = new WebhookAgent();
agent.SaveWebhook(webhook).then(webhook => console.log(webhook.WebhookId));

You connect the language parsing to the current resource manager instance using the global configuration. This request will add SO-Language and SO-Culture headers to the HTTP request, so that the WebAPI will return strings that match the requested language (by using the resource strings and multi-language string parsing.

import { WebApi, WebhookAgent } from "@superoffice/tsclient.webapi";
import { ResourceManager } from "../scil-core/src/Data/ResourceManager";

let rm = ResourceManager.GetInstance();
WebApi.configure("http://example.com/SuperOffice", rm);

let agent = new WebhookAgent();
let hook = await agent.CreateDefaultWebhook();

// HTTP request:
// POST http://example.com/SuperOffice/api/v1/Webhook/CreateDefaultWebhook
// SO-Language: en-us

You can disable string substitution for an agent instance by passing in an enum value:

WebApi.configure("http://example.com/SuperOffice", "en");

let agent = new WebhookAgent(ResourceParsing.DoNotParse);
let hook = await agent.CreateDefaultWebhook();

// HTTP request:
// POST http://example.com/SuperOffice/api/v1/Webhook/CreateDefaultWebhook
// SO-Language: -

You can cancel a request by providing an AbortController.

let agent = new WebhookAgent();
let aborter = agent.MakeAbortController();
let options: WebApiRequestOptions = { abortController: aborter };
agent
  .CreateDefaultWebhook(options)
  .then(response => console.log(response))
  .catch(err => {
    if (options.requestStatus === WebApiStatus.Cancelled) {
      console.error("Request was cancelled");
      done();
    }
  });

// Abort the request
aborter.abort();

Misc Types

  • DateTime is converted to TS date
  • TimeSpan is converted to a momentjs.duration
  • Blobs are converted to ArrayBuffers.

License

Proprietary.

Copyright (c) 2020, SuperOffice AS

Readme

Keywords

none

Package Sidebar

Install

npm i @superoffice/tsclient.webapi

Weekly Downloads

13

Version

10.2.6-1262

License

ISC

Unpacked Size

18.6 MB

Total Files

495

Last publish

Collaborators

  • ejfasting
  • josteink
  • sodevnet