Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add basic typescript sample and emit additional types #2561

Merged
merged 4 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,28 @@ This will return an object with the API name as object property names, and an ar
This library is written in TypeScript, and provides types out of the box. All classes and interfaces generated for each API are exported under the `${apiName}_${version}` namespace. For example, the Drive v3 API types are all available from the `drive_v3` namespace:

```ts
import { drive_v3 } from 'googleapis';
import {
google, // The top level object used to access services
drive_v3, // For every service client, there is an exported namespace
Auth, // Namespace for auth related types
Common, // General types used throughout the library
} from 'googleapis';

// Note: using explicit types like `Auth.GoogleAuth` are only here for
// demonstration purposes. Generally with TypeScript, these types would
// be inferred.
const auth: Auth.GoogleAuth = new google.auth.GoogleAuth();
const drive: drive_v3.Drive = google.drive({
version: 'v3',
auth,
});

// There are generated types for every set of request parameters
const listParams: drive_v3.Params$Resource$Files$List = {};
const res = await drive.files.list(listParams);

// There are generated types for the response fields as well
const listResults: drive_v3.Schema$FileList = res.data;
```

### HTTP/2
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
],
"dependencies": {
"google-auth-library": "^7.0.2",
"googleapis-common": "^5.0.1"
"googleapis-common": "^5.0.2"
},
"devDependencies": {
"@compodoc/compodoc": "^1.1.10",
Expand Down
4 changes: 3 additions & 1 deletion samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"!test/"
],
"scripts": {
"prepare": "cd typescript && tsc && cd ..",
"test": "mocha"
},
"dependencies": {
Expand All @@ -27,6 +28,7 @@
"execa": "^5.0.0",
"mocha": "^8.0.0",
"nock": "^13.0.0",
"proxyquire": "^2.1.3"
"proxyquire": "^2.1.3",
"typescript": "~4.2.3"
}
}
8 changes: 8 additions & 0 deletions samples/typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"strict": true,
"outDir": "build"
}
}
49 changes: 49 additions & 0 deletions samples/typescript/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2021 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {
google, // The top level object used to access services
drive_v3, // For every service client, there is an exported namespace
Auth, // Namespace for auth related types
Common, // General types used throughout the library
} from 'googleapis';

async function main() {
// Note: using explicit types like `Auth.GoogleAuth` is only here for
// demonstration purposes. Generally with TypeScript, these types would
// be inferred.
const auth: Auth.GoogleAuth = new google.auth.GoogleAuth();
const drive: drive_v3.Drive = google.drive({
version: 'v3',
auth,
});

try {
// There are generated types for every set of request parameters
const listParams: drive_v3.Params$Resource$Files$List = {};
const res = await drive.files.list(listParams);

// There are generated types for the response fields as well
const listResults: drive_v3.Schema$FileList = res.data;
console.log(listResults);
} catch (e) {
// In many cases, errors from the API will come back as `GaxiosError`.
// These will include the full HTTP Response (you should check for it first)
if (e.response) {
const err = e as Common.GaxiosError;
console.error(err.response);
throw err;
}
}
}
main();