1.0.0 • Published 5 years ago
apps-script-rest-client v1.0.0
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:
- Click on the menu item "Resources > Libraries..."
- In the "Find a Library" text box, enter the script ID
1up4VsouP5BLfhpNVILoSZgLaEtSq9emhecoTHP6K5oMOEhzCXnfLYAA4
and click the "Select" button. - Choose a version in the dropdown box (usually best to pick the latest version).
- 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