endpoint.js
4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"use strict";
// 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
//
// 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.
Object.defineProperty(exports, "__esModule", { value: true });
const apirequest_1 = require("./apirequest");
class Endpoint {
constructor(options) {
this._options = options || {};
}
/**
* Given a schema, add methods and resources to a target.
*
* @param {object} target The target to which to apply the schema.
* @param {object} rootSchema The top-level schema, so we don't lose track of it
* during recursion.
* @param {object} schema The current schema from which to extract methods and
* resources.
* @param {object} context The context to add to each method.
*/
applySchema(target, rootSchema, schema, context) {
this.applyMethodsFromSchema(target, rootSchema, schema, context);
if (schema.resources) {
for (const resourceName in schema.resources) {
if (schema.resources.hasOwnProperty(resourceName)) {
const resource = schema.resources[resourceName];
if (!target[resourceName]) {
target[resourceName] = {};
}
this.applySchema(target[resourceName], rootSchema, resource, context);
}
}
}
}
/**
* Given a schema, add methods to a target.
*
* @param {object} target The target to which to apply the methods.
* @param {object} rootSchema The top-level schema, so we don't lose track of it
* during recursion.
* @param {object} schema The current schema from which to extract methods.
* @param {object} context The context to add to each method.
*/
applyMethodsFromSchema(target, rootSchema, schema, context) {
if (schema.methods) {
for (const name in schema.methods) {
if (schema.methods.hasOwnProperty(name)) {
const method = schema.methods[name];
target[name] = this.makeMethod(rootSchema, method, context);
}
}
}
}
/**
* Given a method schema, add a method to a target.
*
* @param target The target to which to add the method.
* @param schema The top-level schema that contains the rootUrl, etc.
* @param method The method schema from which to generate the method.
* @param context The context to add to the method.
*/
makeMethod(schema, method, context) {
return (paramsOrCallback, callback) => {
const params = typeof paramsOrCallback === 'function' ? {} : paramsOrCallback;
callback = typeof paramsOrCallback === 'function' ?
paramsOrCallback :
callback;
const schemaUrl = buildurl(schema.rootUrl + schema.servicePath + method.path);
const parameters = {
options: {
url: schemaUrl.substring(1, schemaUrl.length - 1),
method: method.httpMethod
},
params,
requiredParams: method.parameterOrder || [],
pathParams: this.getPathParams(method.parameters),
context
};
if (method.mediaUpload && method.mediaUpload.protocols &&
method.mediaUpload.protocols.simple &&
method.mediaUpload.protocols.simple.path) {
const mediaUrl = buildurl(schema.rootUrl + method.mediaUpload.protocols.simple.path);
parameters.mediaUrl = mediaUrl.substring(1, mediaUrl.length - 1);
}
if (!callback) {
return apirequest_1.createAPIRequest(parameters);
}
apirequest_1.createAPIRequest(parameters, callback);
return;
};
}
getPathParams(params) {
const pathParams = new Array();
if (typeof params !== 'object') {
params = {};
}
Object.keys(params).forEach(key => {
if (params[key].location === 'path') {
pathParams.push(key);
}
});
return pathParams;
}
}
exports.Endpoint = Endpoint;
/**
* Build a string used to create a URL from the discovery doc provided URL.
* replace double slashes with single slash (except in https://)
* @private
* @param input URL to build from
* @return Resulting built URL
*/
function buildurl(input) {
return input ? `'${input}'`.replace(/([^:]\/)\/+/g, '$1') : '';
}
//# sourceMappingURL=endpoint.js.map