설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

apps.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.deleteAppAndroidSha = exports.createAppAndroidSha = exports.listAppAndroidSha = exports.getAppConfig = exports.getAppConfigFile = exports.listFirebaseApps = exports.createWebApp = exports.createAndroidApp = exports.createIosApp = exports.getAppPlatform = exports.ShaCertificateType = exports.AppPlatform = exports.APP_LIST_PAGE_SIZE = void 0;
  4. const fs = require("fs");
  5. const apiv2_1 = require("../apiv2");
  6. const api_1 = require("../api");
  7. const error_1 = require("../error");
  8. const logger_1 = require("../logger");
  9. const operation_poller_1 = require("../operation-poller");
  10. const TIMEOUT_MILLIS = 30000;
  11. exports.APP_LIST_PAGE_SIZE = 100;
  12. const CREATE_APP_API_REQUEST_TIMEOUT_MILLIS = 15000;
  13. const WEB_CONFIG_FILE_NAME = "google-config.js";
  14. var AppPlatform;
  15. (function (AppPlatform) {
  16. AppPlatform["PLATFORM_UNSPECIFIED"] = "PLATFORM_UNSPECIFIED";
  17. AppPlatform["IOS"] = "IOS";
  18. AppPlatform["ANDROID"] = "ANDROID";
  19. AppPlatform["WEB"] = "WEB";
  20. AppPlatform["ANY"] = "ANY";
  21. })(AppPlatform = exports.AppPlatform || (exports.AppPlatform = {}));
  22. var ShaCertificateType;
  23. (function (ShaCertificateType) {
  24. ShaCertificateType["SHA_CERTIFICATE_TYPE_UNSPECIFIED"] = "SHA_CERTIFICATE_TYPE_UNSPECIFIED";
  25. ShaCertificateType["SHA_1"] = "SHA_1";
  26. ShaCertificateType["SHA_256"] = "SHA_256";
  27. })(ShaCertificateType = exports.ShaCertificateType || (exports.ShaCertificateType = {}));
  28. function getAppPlatform(platform) {
  29. switch (platform.toUpperCase()) {
  30. case "IOS":
  31. return AppPlatform.IOS;
  32. case "ANDROID":
  33. return AppPlatform.ANDROID;
  34. case "WEB":
  35. return AppPlatform.WEB;
  36. case "":
  37. return AppPlatform.ANY;
  38. default:
  39. throw new error_1.FirebaseError("Unexpected platform. Only iOS, Android, and Web apps are supported");
  40. }
  41. }
  42. exports.getAppPlatform = getAppPlatform;
  43. const apiClient = new apiv2_1.Client({ urlPrefix: api_1.firebaseApiOrigin, apiVersion: "v1beta1" });
  44. async function createIosApp(projectId, options) {
  45. try {
  46. const response = await apiClient.request({
  47. method: "POST",
  48. path: `/projects/${projectId}/iosApps`,
  49. timeout: CREATE_APP_API_REQUEST_TIMEOUT_MILLIS,
  50. body: options,
  51. });
  52. const appData = await (0, operation_poller_1.pollOperation)({
  53. pollerName: "Create iOS app Poller",
  54. apiOrigin: api_1.firebaseApiOrigin,
  55. apiVersion: "v1beta1",
  56. operationResourceName: response.body.name,
  57. });
  58. return appData;
  59. }
  60. catch (err) {
  61. logger_1.logger.debug(err.message);
  62. throw new error_1.FirebaseError(`Failed to create iOS app for project ${projectId}. See firebase-debug.log for more info.`, { exit: 2, original: err });
  63. }
  64. }
  65. exports.createIosApp = createIosApp;
  66. async function createAndroidApp(projectId, options) {
  67. try {
  68. const response = await apiClient.request({
  69. method: "POST",
  70. path: `/projects/${projectId}/androidApps`,
  71. timeout: CREATE_APP_API_REQUEST_TIMEOUT_MILLIS,
  72. body: options,
  73. });
  74. const appData = await (0, operation_poller_1.pollOperation)({
  75. pollerName: "Create Android app Poller",
  76. apiOrigin: api_1.firebaseApiOrigin,
  77. apiVersion: "v1beta1",
  78. operationResourceName: response.body.name,
  79. });
  80. return appData;
  81. }
  82. catch (err) {
  83. logger_1.logger.debug(err.message);
  84. throw new error_1.FirebaseError(`Failed to create Android app for project ${projectId}. See firebase-debug.log for more info.`, {
  85. exit: 2,
  86. original: err,
  87. });
  88. }
  89. }
  90. exports.createAndroidApp = createAndroidApp;
  91. async function createWebApp(projectId, options) {
  92. try {
  93. const response = await apiClient.request({
  94. method: "POST",
  95. path: `/projects/${projectId}/webApps`,
  96. timeout: CREATE_APP_API_REQUEST_TIMEOUT_MILLIS,
  97. body: options,
  98. });
  99. const appData = await (0, operation_poller_1.pollOperation)({
  100. pollerName: "Create Web app Poller",
  101. apiOrigin: api_1.firebaseApiOrigin,
  102. apiVersion: "v1beta1",
  103. operationResourceName: response.body.name,
  104. });
  105. return appData;
  106. }
  107. catch (err) {
  108. logger_1.logger.debug(err.message);
  109. throw new error_1.FirebaseError(`Failed to create Web app for project ${projectId}. See firebase-debug.log for more info.`, { exit: 2, original: err });
  110. }
  111. }
  112. exports.createWebApp = createWebApp;
  113. function getListAppsResourceString(projectId, platform) {
  114. let resourceSuffix;
  115. switch (platform) {
  116. case AppPlatform.IOS:
  117. resourceSuffix = "/iosApps";
  118. break;
  119. case AppPlatform.ANDROID:
  120. resourceSuffix = "/androidApps";
  121. break;
  122. case AppPlatform.WEB:
  123. resourceSuffix = "/webApps";
  124. break;
  125. case AppPlatform.ANY:
  126. resourceSuffix = ":searchApps";
  127. break;
  128. default:
  129. throw new error_1.FirebaseError("Unexpected platform. Only support iOS, Android and Web apps");
  130. }
  131. return `/projects/${projectId}${resourceSuffix}`;
  132. }
  133. async function listFirebaseApps(projectId, platform, pageSize = exports.APP_LIST_PAGE_SIZE) {
  134. const apps = [];
  135. try {
  136. let nextPageToken;
  137. do {
  138. const queryParams = { pageSize };
  139. if (nextPageToken) {
  140. queryParams.pageToken = nextPageToken;
  141. }
  142. const response = await apiClient.request({
  143. method: "GET",
  144. path: getListAppsResourceString(projectId, platform),
  145. queryParams,
  146. timeout: TIMEOUT_MILLIS,
  147. });
  148. if (response.body.apps) {
  149. const appsOnPage = response.body.apps.map((app) => (app.platform ? app : Object.assign(Object.assign({}, app), { platform })));
  150. apps.push(...appsOnPage);
  151. }
  152. nextPageToken = response.body.nextPageToken;
  153. } while (nextPageToken);
  154. return apps;
  155. }
  156. catch (err) {
  157. logger_1.logger.debug(err.message);
  158. throw new error_1.FirebaseError(`Failed to list Firebase ${platform === AppPlatform.ANY ? "" : platform + " "}` +
  159. "apps. See firebase-debug.log for more info.", {
  160. exit: 2,
  161. original: err,
  162. });
  163. }
  164. }
  165. exports.listFirebaseApps = listFirebaseApps;
  166. function getAppConfigResourceString(appId, platform) {
  167. let platformResource;
  168. switch (platform) {
  169. case AppPlatform.IOS:
  170. platformResource = "iosApps";
  171. break;
  172. case AppPlatform.ANDROID:
  173. platformResource = "androidApps";
  174. break;
  175. case AppPlatform.WEB:
  176. platformResource = "webApps";
  177. break;
  178. default:
  179. throw new error_1.FirebaseError("Unexpected app platform");
  180. }
  181. return `/projects/-/${platformResource}/${appId}/config`;
  182. }
  183. function parseConfigFromResponse(responseBody, platform) {
  184. if (platform === AppPlatform.WEB) {
  185. const JS_TEMPLATE = fs.readFileSync(__dirname + "/../../templates/setup/web.js", "utf8");
  186. return {
  187. fileName: WEB_CONFIG_FILE_NAME,
  188. fileContents: JS_TEMPLATE.replace("{/*--CONFIG--*/}", JSON.stringify(responseBody, null, 2)),
  189. };
  190. }
  191. else if (platform === AppPlatform.ANDROID || platform === AppPlatform.IOS) {
  192. return {
  193. fileName: responseBody.configFilename,
  194. fileContents: Buffer.from(responseBody.configFileContents, "base64").toString("utf8"),
  195. };
  196. }
  197. throw new error_1.FirebaseError("Unexpected app platform");
  198. }
  199. function getAppConfigFile(config, platform) {
  200. return parseConfigFromResponse(config, platform);
  201. }
  202. exports.getAppConfigFile = getAppConfigFile;
  203. async function getAppConfig(appId, platform) {
  204. try {
  205. const response = await apiClient.request({
  206. method: "GET",
  207. path: getAppConfigResourceString(appId, platform),
  208. timeout: TIMEOUT_MILLIS,
  209. });
  210. return response.body;
  211. }
  212. catch (err) {
  213. logger_1.logger.debug(err.message);
  214. throw new error_1.FirebaseError(`Failed to get ${platform} app configuration. See firebase-debug.log for more info.`, {
  215. exit: 2,
  216. original: err,
  217. });
  218. }
  219. }
  220. exports.getAppConfig = getAppConfig;
  221. async function listAppAndroidSha(projectId, appId) {
  222. const shaCertificates = [];
  223. try {
  224. const response = await apiClient.request({
  225. method: "GET",
  226. path: `/projects/${projectId}/androidApps/${appId}/sha`,
  227. timeout: CREATE_APP_API_REQUEST_TIMEOUT_MILLIS,
  228. });
  229. if (response.body.certificates) {
  230. shaCertificates.push(...response.body.certificates);
  231. }
  232. return shaCertificates;
  233. }
  234. catch (err) {
  235. logger_1.logger.debug(err.message);
  236. throw new error_1.FirebaseError(`Failed to list SHA certificate hashes for Android app ${appId}.` +
  237. " See firebase-debug.log for more info.", {
  238. exit: 2,
  239. original: err,
  240. });
  241. }
  242. }
  243. exports.listAppAndroidSha = listAppAndroidSha;
  244. async function createAppAndroidSha(projectId, appId, options) {
  245. try {
  246. const response = await apiClient.request({
  247. method: "POST",
  248. path: `/projects/${projectId}/androidApps/${appId}/sha`,
  249. body: options,
  250. timeout: CREATE_APP_API_REQUEST_TIMEOUT_MILLIS,
  251. });
  252. const shaCertificate = response.body;
  253. return shaCertificate;
  254. }
  255. catch (err) {
  256. logger_1.logger.debug(err.message);
  257. throw new error_1.FirebaseError(`Failed to create SHA certificate hash for Android app ${appId}. See firebase-debug.log for more info.`, {
  258. exit: 2,
  259. original: err,
  260. });
  261. }
  262. }
  263. exports.createAppAndroidSha = createAppAndroidSha;
  264. async function deleteAppAndroidSha(projectId, appId, shaId) {
  265. try {
  266. await apiClient.request({
  267. method: "DELETE",
  268. path: `/projects/${projectId}/androidApps/${appId}/sha/${shaId}`,
  269. timeout: CREATE_APP_API_REQUEST_TIMEOUT_MILLIS,
  270. });
  271. }
  272. catch (err) {
  273. logger_1.logger.debug(err.message);
  274. throw new error_1.FirebaseError(`Failed to delete SHA certificate hash for Android app ${appId}. See firebase-debug.log for more info.`, {
  275. exit: 2,
  276. original: err,
  277. });
  278. }
  279. }
  280. exports.deleteAppAndroidSha = deleteAppAndroidSha;