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

feat: make it ready for use in browser #69

Merged
merged 15 commits into from Jan 17, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -5,4 +5,6 @@ package-lock.json
yarn.lock
.coverage
.nyc_output
dist
docs
coverage
29 changes: 29 additions & 0 deletions .kokoro/browser-test.sh
@@ -0,0 +1,29 @@
#!/bin/bash

# Copyright 2018 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
#
# https://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.

set -xeo pipefail

export NPM_CONFIG_PREFIX=/home/node/.npm-global

# Setup service account credentials.
export GOOGLE_APPLICATION_CREDENTIALS=${KOKORO_GFILE_DIR}/service-account.json
export GCLOUD_PROJECT=long-door-651

cd $(dirname $0)/..

npm install

npm run browser-test
12 changes: 12 additions & 0 deletions .kokoro/continuous/node8/browser-test.cfg
@@ -0,0 +1,12 @@
# Download resources for system tests (service account key, etc.)
gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/google-cloud-nodejs"

# Configure the docker image for kokoro-trampoline.
env_vars: {
key: "TRAMPOLINE_IMAGE"
value: "gcr.io/cloud-devrel-kokoro-resources/node:8-puppeteer"
}
env_vars: {
key: "TRAMPOLINE_BUILD_FILE"
value: "github/nodejs-googleapis-common/.kokoro/browser-test.sh"
}
12 changes: 12 additions & 0 deletions .kokoro/presubmit/node8/browser-test.cfg
@@ -0,0 +1,12 @@
# Download resources for system tests (service account key, etc.)
gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/google-cloud-nodejs"

# Configure the docker image for kokoro-trampoline.
env_vars: {
key: "TRAMPOLINE_IMAGE"
value: "gcr.io/cloud-devrel-kokoro-resources/node:8-puppeteer"
}
env_vars: {
key: "TRAMPOLINE_BUILD_FILE"
value: "github/nodejs-googleapis-common/.kokoro/browser-test.sh"
}
24 changes: 24 additions & 0 deletions browser-test/test.isbrowser.ts
@@ -0,0 +1,24 @@
/**
* Copyright 2019 Google LLC. All Rights Reserved.
*
* 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 * as assert from 'assert';
import {isBrowser} from '../src/isbrowser';

describe('isbrowser', () => {
it('should detect browser', () => {
assert(isBrowser());
});
});
95 changes: 95 additions & 0 deletions karma.conf.js
@@ -0,0 +1,95 @@
/**
* Copyright 2019 Google LLC. All Rights Reserved.
*
* 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.
*/

// Karma configuration
// Use `npm run browser-test` to run browser tests with Karma.
const isDocker = require('is-docker')();

const webpackConfig = require('./webpack-tests.config.js');
process.env.CHROME_BIN = require('puppeteer').executablePath();

module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha'],

// list of files / patterns to load in the browser
files: ['./browser-test/*.ts'],

// list of files / patterns to exclude
exclude: [],

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'./src/*.ts': ['coverage'],
'./src/**/*.ts': ['coverage'],
'./browser-test/*.ts': ['webpack', 'sourcemap'],
},

webpack: webpackConfig,

// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress', 'coverage', 'remap-coverage'],

coverageReporter: {type: 'in-memory'},
remapCoverageReporter: {html: './coverage'},

// web server port
port: 9876,

// enable / disable colors in the output (reporters and logs)
colors: true,

// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,

// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,

// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['ChromeCustom'],
customLaunchers: {
ChromeCustom: {
base: 'ChromeHeadless',
// We must disable the Chrome sandbox when running Chrome inside Docker (Chrome's sandbox needs
// more permissions than Docker allows by default)
flags: isDocker ? ['--no-sandbox'] : [],
},
},

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,

// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,

// set correct MIME type when serving .ts files (already compiled to JavaScript):
mime: {
'text/javascript': ['ts'],
},
});
};
30 changes: 26 additions & 4 deletions package.json
Expand Up @@ -22,32 +22,54 @@
"posttest": "npm run check",
"lint": "npm run check",
"samples-test": "mocha build/samples-test",
"docs": "compodoc src/"
"docs": "compodoc src/",
"webpack": "webpack",
"browser-test": "karma start"
},
"keywords": [],
"author": "Google LLC",
"license": "Apache-2.0",
"devDependencies": {
"@compodoc/compodoc": "^1.1.7",
"@types/execa": "^0.9.0",
"@types/mocha": "^5.2.5",
"@types/mv": "^2.1.0",
"@types/ncp": "^2.0.1",
"@types/nock": "^9.3.0",
"@types/pify": "^3.0.2",
"@types/qs": "^6.5.1",
"@types/tmp": "0.0.33",
"@types/url-template": "^2.0.28",
"@types/uuid": "^3.4.3",
"codecov": "^3.0.4",
"gts": "^0.9.0",
"ink-docstrap": "^1.3.2",
"intelli-espower-loader": "^1.0.1",
"is-docker": "^1.1.0",
"karma": "^3.1.4",
"karma-chrome-launcher": "^2.2.0",
"karma-coverage": "^1.1.2",
"karma-firefox-launcher": "^1.1.0",
"karma-mocha": "^1.3.0",
"karma-remap-coverage": "^0.1.5",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^3.0.5",
"mocha": "^5.2.0",
"mv": "^2.1.1",
"ncp": "^2.0.0",
"nock": "^10.0.0",
"null-loader": "^0.1.1",
"nyc": "^13.1.0",
"puppeteer": "^1.11.0",
"source-map-support": "^0.5.8",
"typescript": "~3.2.0"
"ts-loader": "^5.3.2",
"typescript": "~3.2.0",
"webpack": "^4.28.3",
"webpack-cli": "^3.2.0"
},
"dependencies": {
"axios": "^0.18.0",
"google-auth-library": "^2.0.0",
"gaxios": "^1.2.2",
"google-auth-library": "^3.0.0",
"pify": "^4.0.0",
"qs": "^6.5.2",
"url-template": "^2.0.8",
Expand Down
47 changes: 7 additions & 40 deletions src/api.ts
Expand Up @@ -11,13 +11,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {AxiosAdapter, AxiosProxyConfig, AxiosRequestConfig, AxiosResponse, AxiosTransformer, CancelToken} from 'axios';
import {GaxiosOptions, GaxiosResponse} from 'gaxios';
import {OAuth2Client} from 'google-auth-library';

import {Endpoint} from './endpoint';

// tslint:disable-next-line no-any
export interface APIRequestParams<T = any> {
options: AxiosRequestConfig;
options: GaxiosOptions;
params: T;
requiredParams: string[];
pathParams: string[];
Expand All @@ -36,55 +37,21 @@ export interface APIRequestContext {

/**
* This interface is a mix of the AxiosRequestConfig options
* and our `auth: OAuth2Client|string` options. We need to redefine
* the interface here because the `auth` property already exists
* on AxiosRequestConfig, and uses an entirely different type.
* and our `auth: OAuth2Client|string` options.
*/
export interface GlobalOptions {
url?: string;
method?: string;
baseURL?: string;
transformRequest?: AxiosTransformer|AxiosTransformer[];
transformResponse?: AxiosTransformer|AxiosTransformer[];
// tslint:disable-next-line no-any
headers?: any;
// tslint:disable-next-line no-any
params?: any;
// tslint:disable-next-line no-any
paramsSerializer?: (params: any) => string;
// tslint:disable-next-line no-any
data?: any;
timeout?: number;
withCredentials?: boolean;
adapter?: AxiosAdapter;
export interface GlobalOptions extends GaxiosOptions {
auth?: OAuth2Client|string;
responseType?: string;
xsrfCookieName?: string;
xsrfHeaderName?: string;
// tslint:disable-next-line no-any
onUploadProgress?: (progressEvent: any) => void;
// tslint:disable-next-line no-any
onDownloadProgress?: (progressEvent: any) => void;
maxContentLength?: number;
validateStatus?: (status: number) => boolean;
maxRedirects?: number;
// tslint:disable-next-line no-any
httpAgent?: any;
// tslint:disable-next-line no-any
httpsAgent?: any;
proxy?: AxiosProxyConfig|false;
cancelToken?: CancelToken;
}

export interface MethodOptions extends AxiosRequestConfig {
export interface MethodOptions extends GaxiosOptions {
rootUrl?: string;
}
export interface ServiceOptions extends GlobalOptions {
version?: string;
}

export type BodyResponseCallback<T> =
(err: Error|null, res?: AxiosResponse<T>|null) => void;
(err: Error|null, res?: GaxiosResponse<T>|null) => void;

// tslint:disable-next-line: no-any
export type APIEndpoint = Readonly<Endpoint&any>;
22 changes: 9 additions & 13 deletions src/apirequest.ts
Expand Up @@ -11,19 +11,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {AxiosPromise} from 'axios';
import {GaxiosPromise} from 'gaxios';
import {DefaultTransporter, OAuth2Client} from 'google-auth-library';
import {BodyResponseCallback} from 'google-auth-library/build/src/transporters';
import * as qs from 'qs';
import * as stream from 'stream';
import * as urlTemplate from 'url-template';
import * as uuid from 'uuid';

import {APIRequestParams, GlobalOptions} from './api';
import {APIRequestParams, BodyResponseCallback} from './api';
import {isBrowser} from './isbrowser';
import {SchemaParameters} from './schema';

const maxContentLength = Math.pow(2, 31);

// tslint:disable-next-line no-var-requires
const pkg = require('../../package.json');
const USER_AGENT = `google-api-nodejs-client/${pkg.version} (gzip)`;
Expand Down Expand Up @@ -52,12 +50,12 @@ function getMissingParams(params: SchemaParameters, required: string[]) {
* @param callback Callback when request finished or error found
*/
export function createAPIRequest<T>(parameters: APIRequestParams):
AxiosPromise<T>;
GaxiosPromise<T>;
export function createAPIRequest<T>(
parameters: APIRequestParams, callback: BodyResponseCallback<T>): void;
export function createAPIRequest<T>(
parameters: APIRequestParams, callback?: BodyResponseCallback<T>): void|
AxiosPromise<T> {
GaxiosPromise<T> {
if (callback) {
createAPIRequestAsync<T>(parameters).then(r => callback(null, r), callback);
} else {
Expand Down Expand Up @@ -223,12 +221,10 @@ async function createAPIRequestAsync<T>(parameters: APIRequestParams) {

options.headers = headers;
options.params = params;
// We need to set a default content size, or the max defaults
// to 10MB. Setting to 2GB by default.
// https://github.com/google/google-api-nodejs-client/issues/991
options.maxContentLength = options.maxContentLength || maxContentLength;
options.headers['Accept-Encoding'] = 'gzip';
options.headers['User-Agent'] = USER_AGENT;
if (!isBrowser()) {
options.headers!['Accept-Encoding'] = 'gzip';
options.headers!['User-Agent'] = USER_AGENT;
}

// By default Axios treats any 2xx as valid, and all non 2xx status
// codes as errors. This is a problem for HTTP 304s when used along
Expand Down
5 changes: 4 additions & 1 deletion src/discovery.ts
Expand Up @@ -12,10 +12,12 @@
// limitations under the License.

import * as fs from 'fs';
import {Headers} from 'gaxios';
import {DefaultTransporter} from 'google-auth-library';
import * as pify from 'pify';
import * as url from 'url';
import * as util from 'util';

import {GlobalOptions, ServiceOptions} from './api';
import {createAPIRequest} from './apirequest';
import {Endpoint} from './endpoint';
Expand Down Expand Up @@ -71,7 +73,8 @@ export class Discovery {
* @param discoveryUrl
*/
async discoverAllAPIs(discoveryUrl: string): Promise<{}> {
const headers = this.options.includePrivate ? {} : {'X-User-Ip': '0.0.0.0'};
const headers: Headers =
this.options.includePrivate ? {} : {'X-User-Ip': '0.0.0.0'};
const res =
await this.transporter.request<Schemas>({url: discoveryUrl, headers});
const items = res.data.items;
Expand Down