0.0.3 • Published 4 years ago
@wnagasawa/apps-script-rest-client v0.0.3
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
1mvxMPd7MWgOeQmOzovVgU3yXcSlnPZ74tz1tjxNI-zMs4wO_xcm5aTJq
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
Add Typescript Definitions:
- Add the package:
npm install -D @wnagasawa/apps-script-rest-client
- Configure tsconfig.json:
{ "compilerOptions": { "typeRoots" : ["node_modules/@wnagasawa", "node_modules/@types"] } }
Usage
1. Base URL
abstract class GitHubRequest<T> extends RestClient.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. Sending request
import { GitHubAPI } from './GitHubAPI'
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)
}