fallbackRest.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. /**
  3. * Copyright 2021 Google LLC
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. Object.defineProperty(exports, "__esModule", { value: true });
  18. exports.encodeRequest = encodeRequest;
  19. exports.decodeResponse = decodeResponse;
  20. // proto-over-HTTP request encoding and decoding
  21. const serializer = require("proto3-json-serializer");
  22. const fallback_1 = require("./fallback");
  23. const googleError_1 = require("./googleError");
  24. const transcoding_1 = require("./transcoding");
  25. function encodeRequest(rpc, protocol, servicePath, servicePort, request, numericEnums) {
  26. const headers = {
  27. 'Content-Type': 'application/json',
  28. };
  29. const message = rpc.resolvedRequestType.fromObject(request);
  30. const json = serializer.toProto3JSON(message, {
  31. numericEnums,
  32. });
  33. if (!json) {
  34. throw new Error(`Cannot send null request to RPC ${rpc.name}.`);
  35. }
  36. if (typeof json !== 'object' || Array.isArray(json)) {
  37. throw new Error(`Request to RPC ${rpc.name} must be an object.`);
  38. }
  39. const transcoded = (0, transcoding_1.transcode)(json, rpc.parsedOptions);
  40. if (!transcoded) {
  41. throw new Error(`Cannot build HTTP request for ${JSON.stringify(json)}, method: ${rpc.name}`);
  42. }
  43. // If numeric enums feature is requested, add extra parameter to the query string
  44. if (numericEnums) {
  45. transcoded.queryString =
  46. (transcoded.queryString ? `${transcoded.queryString}&` : '') +
  47. '$alt=json%3Benum-encoding=int';
  48. }
  49. // Converts httpMethod to method that permitted in standard Fetch API spec
  50. // https://fetch.spec.whatwg.org/#methods
  51. const method = transcoded.httpMethod.toUpperCase();
  52. const body = JSON.stringify(transcoded.data);
  53. const url = `${protocol}://${servicePath}:${servicePort}/${transcoded.url.replace(/^\//, '')}?${transcoded.queryString}`;
  54. return {
  55. method,
  56. url,
  57. headers,
  58. body,
  59. };
  60. }
  61. function decodeResponse(rpc, ok, response) {
  62. // eslint-disable-next-line n/no-unsupported-features/node-builtins
  63. const decodedString = new TextDecoder().decode(response);
  64. const json = JSON.parse(decodedString);
  65. if (!ok) {
  66. const error = googleError_1.GoogleError.parseHttpError(json);
  67. throw error;
  68. }
  69. const message = serializer.fromProto3JSON(rpc.resolvedResponseType, json);
  70. if (!message) {
  71. throw new Error(`Received null response from RPC ${rpc.name}`);
  72. }
  73. return rpc.resolvedResponseType.toObject(message, fallback_1.defaultToObjectOptions);
  74. }
  75. //# sourceMappingURL=fallbackRest.js.map