Skip to content

Commit

Permalink
fix: add basic typescript sample and emit additional types (#2561)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Mar 24, 2021
1 parent 084e3e2 commit ed0e1ab
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
23 changes: 22 additions & 1 deletion README.md
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
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
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
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"strict": true,
"outDir": "build"
}
}
49 changes: 49 additions & 0 deletions samples/typescript/typescript.ts
@@ -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();

0 comments on commit ed0e1ab

Please sign in to comment.