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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use prettier and eslint on samples #1341

Merged
merged 1 commit into from Sep 18, 2018
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
3 changes: 3 additions & 0 deletions .prettierignore
@@ -0,0 +1,3 @@
node_modules/*
samples/node_modules/*
src/**/doc/*
8 changes: 8 additions & 0 deletions .prettierrc
@@ -0,0 +1,8 @@
---
bracketSpacing: false
printWidth: 80
semi: true
singleQuote: true
tabWidth: 2
trailingComma: es5
useTabs: false
34 changes: 10 additions & 24 deletions package.json
Expand Up @@ -17,20 +17,6 @@
"build/src",
"package.json"
],
"semistandard": {
"ignore": [
"apis",
"templates/*"
],
"globals": [
"after",
"afterEach",
"before",
"beforeEach",
"describe",
"it"
]
},
"repository": "google/google-api-nodejs-client",
"name": "googleapis",
"contributors": [
Expand Down Expand Up @@ -71,15 +57,16 @@
"docs": "echo no docs 馃懟",
"system-test": "echo no system tests 馃懟",
"samples-test": "cd samples && npm link ../ && pwd && npm test",
"lint": "gts check && semistandard 'samples/**/*.js'",
"lint": "gts check && eslint 'samples/**/*.js'",
"compile": "tsc -v && tsc -p . && copyfiles src/apis/**/README.md src/apis/**/package.json build",
"build-tools": "tsc -p tsconfig.tools.json",
"clean": "gts clean",
"codecov": "nyc --cache mocha build/test -t 10000 -S -R spec && nyc report --reporter=html",
"fix": "semistandard --fix 'samples/**/*.js' && gts fix",
"fix": "eslint --fix 'samples/**/*.js' && prettier --write samples/**/*.js && gts fix",
"pregenerate": "npm run build-tools",
"generate": "node build/src/generator/generate.js",
"postgenerate": "npm run fix"
"postgenerate": "npm run fix",
"prettier": "prettier --write samples/**/*.js"
},
"author": "Google Inc.",
"keywords": [
Expand All @@ -96,7 +83,6 @@
"node": ">=6.0"
},
"devDependencies": {
"@types/express": "^4.11.1",
"@types/minimist": "^1.2.0",
"@types/mkdirp": "^0.5.2",
"@types/mocha": "^5.2.0",
Expand All @@ -112,12 +98,14 @@
"@types/source-map-support": "^0.4.0",
"@types/tmp": "^0.0.33",
"@types/url-template": "^2.0.28",
"@types/uuid": "^3.4.3",
"assert-rejects": "^1.0.0",
"axios": "^0.18.0",
"codecov": "^3.0.2",
"copyfiles": "^2.0.0",
"express": "^4.16.3",
"eslint": "^5.6.0",
"eslint-config-prettier": "^3.0.1",
"eslint-plugin-node": "^7.0.1",
"eslint-plugin-prettier": "^2.6.2",
"gts": "^0.8.0",
"hard-rejection": "^1.0.0",
"intelli-espower-loader": "^1.0.1",
Expand All @@ -126,20 +114,18 @@
"mkdirp": "^0.5.1",
"mocha": "^5.1.1",
"mv": "^2.1.1",
"nconf": "^0.10.0",
"ncp": "^2.0.0",
"nock": "^9.2.5",
"nunjucks": "^3.1.2",
"nyc": "^13.0.0",
"opn": "^5.3.0",
"p-queue": "^3.0.0",
"pify": "^4.0.0",
"prettier": "^1.14.2",
"rimraf": "^2.6.2",
"semistandard": "^12.0.1",
"server-destroy": "^1.0.1",
"source-map-support": "^0.5.5",
"tmp": "^0.0.33",
"typescript": "~3.0.0",
"uuid": "^3.2.1"
"typescript": "~3.0.0"
}
}
3 changes: 3 additions & 0 deletions samples/.eslintrc.yml
@@ -0,0 +1,3 @@
---
rules:
no-console: off
29 changes: 14 additions & 15 deletions samples/analytics/analytics.js
Expand Up @@ -18,7 +18,7 @@ const sampleClient = require('../sampleclient');

const analytics = google.analytics({
version: 'v3',
auth: sampleClient.oAuth2Client
auth: sampleClient.oAuth2Client,
});

// Custom Goals must exist prior to being used as an objectiveMetric
Expand All @@ -31,32 +31,31 @@ const servingFramework = 'API';
// Invalid URLs are used when user is not redirected when showing an experiment
// Read more: http://goo.gl/oVwKH1
const variations = [
{'name': 'Default', 'url': 'http://www.example.com', 'status': 'ACTIVE'},
{'name': 'variation 1', 'url': 'http://www.1.com', 'status': 'ACTIVE'},
{'name': 'variation 2', 'url': 'http://www.2.com', 'status': 'ACTIVE'}
{name: 'Default', url: 'http://www.example.com', status: 'ACTIVE'},
{name: 'variation 1', url: 'http://www.1.com', status: 'ACTIVE'},
{name: 'variation 2', url: 'http://www.2.com', status: 'ACTIVE'},
];

async function runSample () {
async function runSample() {
const res = await analytics.management.experiments.insert({
accountId: 'your-accountId',
webPropertyId: 'your-webPropertyId',
profileId: 'your-profileId',
requestBody: {
'name': 'Example Experiment',
'status': 'READY_TO_RUN',
'objectiveMetric': objectiveMetric,
'servingFramework': servingFramework,
'variations': variations
}
name: 'Example Experiment',
status: 'READY_TO_RUN',
objectiveMetric: objectiveMetric,
servingFramework: servingFramework,
variations: variations,
},
});
console.log(res.data);
return res.data;
}

const scopes = [
'https://www.googleapis.com/auth/analytics'
];
const scopes = ['https://www.googleapis.com/auth/analytics'];

sampleClient.authenticate(scopes)
sampleClient
.authenticate(scopes)
.then(() => runSample())
.catch(console.error);
52 changes: 28 additions & 24 deletions samples/analyticsReporting/batchGet.js
Expand Up @@ -13,35 +13,38 @@

'use strict';

const { google } = require('googleapis');
const {google} = require('googleapis');
const sampleClient = require('../sampleclient');

const analyticsreporting = google.analyticsreporting({
version: 'v4',
auth: sampleClient.oAuth2Client
auth: sampleClient.oAuth2Client,
});

async function runSample () {
async function runSample() {
const res = await analyticsreporting.reports.batchGet({
requestBody: {
reportRequests: [{
viewId: '65704806',
dateRanges: [
{
startDate: '2018-03-17',
endDate: '2018-03-24'
}, {
startDate: '14daysAgo',
endDate: '7daysAgo'
}
],
metrics: [
{
expression: 'ga:users'
}
]
}]
}
reportRequests: [
{
viewId: '65704806',
dateRanges: [
{
startDate: '2018-03-17',
endDate: '2018-03-24',
},
{
startDate: '14daysAgo',
endDate: '7daysAgo',
},
],
metrics: [
{
expression: 'ga:users',
},
],
},
],
},
});
console.log(res.data);
return res.data;
Expand All @@ -50,13 +53,14 @@ async function runSample () {
// if invoked directly (not tests), authenticate and run the samples
if (module === require.main) {
const scopes = ['https://www.googleapis.com/auth/analytics'];
sampleClient.authenticate(scopes)
.then(c => runSample())
sampleClient
.authenticate(scopes)
.then(runSample)
.catch(console.error);
}

// export functions for testing purposes
module.exports = {
runSample,
client: sampleClient.oAuth2Client
client: sampleClient.oAuth2Client,
};
24 changes: 15 additions & 9 deletions samples/blogger/blogger.js
Expand Up @@ -19,14 +19,20 @@ const nconf = require('nconf');
const path = require('path');

// Ex: node blogger.js --api_key "YOUR API KEY"
nconf.argv().env().file(path.join(__dirname, 'config.json'));
nconf
.argv()
.env()
.file(path.join(__dirname, 'config.json'));

blogger.blogs.get({
key: nconf.get('api_key'),
blogId: 3213900
}, (err, res) => {
if (err) {
throw err;
blogger.blogs.get(
{
key: nconf.get('api_key'),
blogId: 3213900,
},
(err, res) => {
if (err) {
throw err;
}
console.log(res.data);
}
console.log(res.data);
});
);
14 changes: 8 additions & 6 deletions samples/blogger/insert.js
Expand Up @@ -18,29 +18,31 @@ const sampleClient = require('../sampleclient');

const blogger = google.blogger({
version: 'v3',
auth: sampleClient.oAuth2Client
auth: sampleClient.oAuth2Client,
});

async function runSample () {
async function runSample() {
const res = await blogger.posts.insert({
blogId: '4340475495955554224',
requestBody: {
title: 'Hello from the googleapis npm module!',
content: 'Visit https://github.com/google/google-api-nodejs-client to learn more!'
}
content:
'Visit https://github.com/google/google-api-nodejs-client to learn more!',
},
});
console.log(res.data);
return res.data;
}

if (module === require.main) {
const scopes = ['https://www.googleapis.com/auth/blogger'];
sampleClient.authenticate(scopes)
sampleClient
.authenticate(scopes)
.then(runSample)
.catch(console.error);
}

module.exports = {
runSample,
client: sampleClient.oAuth2Client
client: sampleClient.oAuth2Client,
};
29 changes: 14 additions & 15 deletions samples/compute/compute.js
Expand Up @@ -13,27 +13,26 @@

'use strict';

const request = require('request');
const axios = require('axios');
const {google} = require('googleapis');
const compute = google.compute('v1');
const uri = 'http://metadata/computeMetadata/v1/project/project-id';
const headers = { 'Metadata-Flavor': 'Google' };

// This example can be run from a GCE VM in your project. The example fetches
// your project ID from the VM's metadata server, and then uses the Compute API
// to fetch the list of GCE zones in your project.
//
// See the defaultauth.js sample for an alternate way of fetching compute credentials.
//
google.options({ auth: new google.auth.Compute() });
request.get({ uri, headers }, (err, res, project) => {
if (err) {
throw err;
}
if (res.statusCode !== 200) {
throw new Error(`Response failed with status ${res.statusCode}`);
}
compute.zones.list({ project }, (err, res) => {
console.log(err, res.data);
});
});
google.options({auth: new google.auth.Compute()});
async function getZones() {
// Get the projectId from the GCE metadata server
const url = 'http://metadata/computeMetadata/v1/project/project-id';
const headers = {'Metadata-Flavor': 'Google'};
const res = await axios.get({url, headers});
const project = res.data;

const zonesResponse = await compute.zones.list({project});
console.log(zonesResponse.data);
}

getZones().catch(console.error);
8 changes: 4 additions & 4 deletions samples/customsearch/customsearch.js
Expand Up @@ -23,12 +23,12 @@ const customsearch = google.customsearch('v1');
// "API KEY"
// "CUSTOM ENGINE ID"

async function runSample (options) {
async function runSample(options) {
console.log(options);
const res = await customsearch.cse.list({
cx: options.cx,
q: options.q,
auth: options.apiKey
auth: options.apiKey,
});
console.log(res.data);
return res.data;
Expand All @@ -40,11 +40,11 @@ if (module === require.main) {
const options = {
q: process.argv[2],
apiKey: process.argv[3],
cx: process.argv[4]
cx: process.argv[4],
};
runSample(options).catch(console.error);
}

module.exports = {
runSample
runSample,
};
6 changes: 3 additions & 3 deletions samples/defaultauth.js
Expand Up @@ -34,18 +34,18 @@ const compute = google.compute('v1');
*/

// Get the appropriate type of credential client, depending upon the runtime environment.
async function main () {
async function main() {
// The `getClient` method will choose a service based authentication model
const auth = await google.auth.getClient({
// Scopes can be specified either as an array or as a single, space-delimited string.
scopes: ['https://www.googleapis.com/auth/compute']
scopes: ['https://www.googleapis.com/auth/compute'],
});

// Obtain the current project Id
const project = await google.auth.getDefaultProjectId();

// Get the list of available compute zones for your project
const res = await compute.zones.list({ project, auth });
const res = await compute.zones.list({project, auth});
console.log(res.data);
}

Expand Down