1.0.0 • Published 5 years ago

apps-script-rest-client v1.0.0

Weekly downloads
-
License
-
Repository
github
Last release
5 years ago

Google Apps Script REST Client with typings for use with TypeScript

A lightweight REST client optimized for use with TypeScript with generics.

Setup

This library is already published as an Apps Script, making it easy to include in your project. To add it to your script, do the following in the Apps Script code editor:

  1. Click on the menu item "Resources > Libraries..."
  2. In the "Find a Library" text box, enter the script ID 1up4VsouP5BLfhpNVILoSZgLaEtSq9emhecoTHP6K5oMOEhzCXnfLYAA4 and click the "Select" button.
  3. Choose a version in the dropdown box (usually best to pick the latest version).
  4. Click the "Save" button.

If you are setting explicit scopes in your manifest file, ensure that the following scope is included:

  • https://www.googleapis.com/auth/script.external_request

Usage

1. Base URL

import { Request } from './RestClient'

abstract class GitHubRequest<T> extends Request<T> {
  constructor(
    method: 'get' | 'post',
    path: string,
    queryParameters?: { [key: string]: any },
    payload?: string | object | GoogleAppsScript.Base.Blob,
  ) {
    super('https://api.github.com', method, path, queryParameters, payload)
  }
}

2. Defining request types

export namespace GitHubAPI {
  export class SearchRepositoriesRequest extends GitHubRequest<ISearchResponse> {
    constructor(query: string) {
      super(
        'get',
        `/search/repositories`,
        { q: query },
      )
    }
  }
}

export interface ISearchResponse {
  readonly items: IItem[]
  readonly totalCount: number
}

3. Run

import { GitHubAPI } from './GitHubAPI'
import { RestClient } from './RestClient'

function run() {
  const request = new GitHubAPI.SearchRepositoriesRequest('typescript')
  const result = RestClient.send(request)
  if (!result.ok) {
    console.log(result.error)
    return
  }
  const response = result.value
  console.log(response)
}
1.0.0

5 years ago