1 | ;
|
2 | // Copyright 2020 Google LLC
|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
4 | // you may not use this file except in compliance with the License.
|
5 | // You may obtain a copy of the License at
|
6 | //
|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
8 | //
|
9 | // Unless required by applicable law or agreed to in writing, software
|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 | // See the License for the specific language governing permissions and
|
13 | // limitations under the License.
|
14 | Object.defineProperty(exports, "__esModule", { value: true });
|
15 | exports.Endpoint = void 0;
|
16 | const apirequest_1 = require("./apirequest");
|
17 | class Endpoint {
|
18 | constructor(options) {
|
19 | this._options = options || {};
|
20 | }
|
21 | /**
|
22 | * Given a schema, add methods and resources to a target.
|
23 | *
|
24 | * @param {object} target The target to which to apply the schema.
|
25 | * @param {object} rootSchema The top-level schema, so we don't lose track of it
|
26 | * during recursion.
|
27 | * @param {object} schema The current schema from which to extract methods and
|
28 | * resources.
|
29 | * @param {object} context The context to add to each method.
|
30 | */
|
31 | applySchema(target, rootSchema, schema, context) {
|
32 | this.applyMethodsFromSchema(target, rootSchema, schema, context);
|
33 | if (schema.resources) {
|
34 | for (const resourceName in schema.resources) {
|
35 | if (Object.prototype.hasOwnProperty.call(schema.resources, resourceName)) {
|
36 | const resource = schema.resources[resourceName];
|
37 | if (!target[resourceName]) {
|
38 | target[resourceName] = {};
|
39 | }
|
40 | this.applySchema(target[resourceName], rootSchema, resource, context);
|
41 | }
|
42 | }
|
43 | }
|
44 | }
|
45 | /**
|
46 | * Given a schema, add methods to a target.
|
47 | *
|
48 | * @param {object} target The target to which to apply the methods.
|
49 | * @param {object} rootSchema The top-level schema, so we don't lose track of it
|
50 | * during recursion.
|
51 | * @param {object} schema The current schema from which to extract methods.
|
52 | * @param {object} context The context to add to each method.
|
53 | */
|
54 | applyMethodsFromSchema(target, rootSchema, schema, context) {
|
55 | if (schema.methods) {
|
56 | for (const name in schema.methods) {
|
57 | if (Object.prototype.hasOwnProperty.call(schema.methods, name)) {
|
58 | const method = schema.methods[name];
|
59 | target[name] = this.makeMethod(rootSchema, method, context);
|
60 | }
|
61 | }
|
62 | }
|
63 | }
|
64 | /**
|
65 | * Given a method schema, add a method to a target.
|
66 | *
|
67 | * @param target The target to which to add the method.
|
68 | * @param schema The top-level schema that contains the rootUrl, etc.
|
69 | * @param method The method schema from which to generate the method.
|
70 | * @param context The context to add to the method.
|
71 | */
|
72 | makeMethod(schema, method, context) {
|
73 | return (paramsOrCallback, callback) => {
|
74 | const params = typeof paramsOrCallback === 'function' ? {} : paramsOrCallback;
|
75 | callback =
|
76 | typeof paramsOrCallback === 'function'
|
77 | ? paramsOrCallback
|
78 | : callback;
|
79 | const schemaUrl = buildurl(schema.rootUrl + schema.servicePath + method.path);
|
80 | const parameters = {
|
81 | options: {
|
82 | url: schemaUrl.substring(1, schemaUrl.length - 1),
|
83 | method: method.httpMethod,
|
84 | },
|
85 | params,
|
86 | requiredParams: method.parameterOrder || [],
|
87 | pathParams: this.getPathParams(method.parameters),
|
88 | context,
|
89 | };
|
90 | if (method.mediaUpload &&
|
91 | method.mediaUpload.protocols &&
|
92 | method.mediaUpload.protocols.simple &&
|
93 | method.mediaUpload.protocols.simple.path) {
|
94 | const mediaUrl = buildurl(schema.rootUrl + method.mediaUpload.protocols.simple.path);
|
95 | parameters.mediaUrl = mediaUrl.substring(1, mediaUrl.length - 1);
|
96 | }
|
97 | if (!callback) {
|
98 | return (0, apirequest_1.createAPIRequest)(parameters);
|
99 | }
|
100 | (0, apirequest_1.createAPIRequest)(parameters, callback);
|
101 | return;
|
102 | };
|
103 | }
|
104 | getPathParams(params) {
|
105 | const pathParams = new Array();
|
106 | if (typeof params !== 'object') {
|
107 | params = {};
|
108 | }
|
109 | Object.keys(params).forEach(key => {
|
110 | if (params[key].location === 'path') {
|
111 | pathParams.push(key);
|
112 | }
|
113 | });
|
114 | return pathParams;
|
115 | }
|
116 | }
|
117 | exports.Endpoint = Endpoint;
|
118 | /**
|
119 | * Build a string used to create a URL from the discovery doc provided URL.
|
120 | * replace double slashes with single slash (except in https://)
|
121 | * @private
|
122 | * @param input URL to build from
|
123 | * @return Resulting built URL
|
124 | */
|
125 | function buildurl(input) {
|
126 | return input ? `'${input}'`.replace(/([^:]\/)\/+/g, '$1') : '';
|
127 | }
|
128 | //# sourceMappingURL=endpoint.js.map |
\ | No newline at end of file |