@zapier/zapier-sdk 0.8.3 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +10 -33
  3. package/dist/index.cjs +346 -219
  4. package/dist/index.d.mts +181 -185
  5. package/dist/index.d.ts +1 -2
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +0 -1
  8. package/dist/index.mjs +343 -218
  9. package/dist/plugins/apps/index.d.ts +4 -0
  10. package/dist/plugins/apps/index.d.ts.map +1 -1
  11. package/dist/plugins/getApp/index.d.ts +2 -7
  12. package/dist/plugins/getApp/index.d.ts.map +1 -1
  13. package/dist/plugins/getApp/index.js +9 -9
  14. package/dist/plugins/getApp/index.test.js +1 -1
  15. package/dist/plugins/getAuthentication/index.test.js +1 -1
  16. package/dist/plugins/listActions/index.d.ts +2 -4
  17. package/dist/plugins/listActions/index.d.ts.map +1 -1
  18. package/dist/plugins/listActions/index.js +1 -1
  19. package/dist/plugins/listActions/index.test.js +4 -4
  20. package/dist/plugins/listApps/index.d.ts +4 -7
  21. package/dist/plugins/listApps/index.d.ts.map +1 -1
  22. package/dist/plugins/listApps/index.js +33 -17
  23. package/dist/plugins/listApps/index.test.js +22 -2
  24. package/dist/plugins/listAuthentications/index.d.ts +2 -4
  25. package/dist/plugins/listAuthentications/index.d.ts.map +1 -1
  26. package/dist/plugins/listAuthentications/index.js +4 -0
  27. package/dist/plugins/listAuthentications/index.test.js +39 -13
  28. package/dist/plugins/listAuthentications/schemas.d.ts +3 -0
  29. package/dist/plugins/listAuthentications/schemas.d.ts.map +1 -1
  30. package/dist/plugins/listAuthentications/schemas.js +4 -0
  31. package/dist/plugins/manifest/index.d.ts +25 -9
  32. package/dist/plugins/manifest/index.d.ts.map +1 -1
  33. package/dist/plugins/manifest/index.js +239 -67
  34. package/dist/plugins/manifest/index.test.js +426 -171
  35. package/dist/plugins/manifest/schemas.d.ts +5 -1
  36. package/dist/plugins/manifest/schemas.d.ts.map +1 -1
  37. package/dist/plugins/manifest/schemas.js +1 -0
  38. package/dist/sdk.d.ts +5 -11
  39. package/dist/sdk.d.ts.map +1 -1
  40. package/dist/sdk.js +1 -4
  41. package/dist/types/plugin.d.ts +1 -0
  42. package/dist/types/plugin.d.ts.map +1 -1
  43. package/dist/types/sdk.d.ts +6 -3
  44. package/dist/types/sdk.d.ts.map +1 -1
  45. package/dist/utils/domain-utils.d.ts +16 -0
  46. package/dist/utils/domain-utils.d.ts.map +1 -1
  47. package/dist/utils/domain-utils.js +46 -27
  48. package/dist/utils/domain-utils.test.js +157 -3
  49. package/dist/utils/file-utils.d.ts +4 -0
  50. package/dist/utils/file-utils.d.ts.map +1 -0
  51. package/dist/utils/file-utils.js +74 -0
  52. package/dist/utils/file-utils.test.d.ts +2 -0
  53. package/dist/utils/file-utils.test.d.ts.map +1 -0
  54. package/dist/utils/file-utils.test.js +51 -0
  55. package/package.json +1 -1
  56. package/src/index.ts +1 -1
  57. package/src/plugins/apps/index.ts +9 -2
  58. package/src/plugins/getApp/index.test.ts +1 -1
  59. package/src/plugins/getApp/index.ts +12 -14
  60. package/src/plugins/getAuthentication/index.test.ts +1 -1
  61. package/src/plugins/listActions/index.test.ts +8 -7
  62. package/src/plugins/listActions/index.ts +3 -3
  63. package/src/plugins/listApps/index.test.ts +23 -2
  64. package/src/plugins/listApps/index.ts +46 -25
  65. package/src/plugins/listAuthentications/index.test.ts +52 -15
  66. package/src/plugins/listAuthentications/index.ts +7 -2
  67. package/src/plugins/listAuthentications/schemas.ts +4 -0
  68. package/src/plugins/manifest/index.test.ts +503 -197
  69. package/src/plugins/manifest/index.ts +338 -82
  70. package/src/plugins/manifest/schemas.ts +9 -2
  71. package/src/sdk.ts +1 -5
  72. package/src/types/plugin.ts +3 -0
  73. package/src/types/sdk.ts +26 -21
  74. package/src/utils/domain-utils.test.ts +196 -2
  75. package/src/utils/domain-utils.ts +68 -35
  76. package/src/utils/file-utils.test.ts +73 -0
  77. package/src/utils/file-utils.ts +94 -0
  78. package/tsconfig.tsbuildinfo +1 -1
  79. package/dist/plugins/lockVersion/index.d.ts +0 -24
  80. package/dist/plugins/lockVersion/index.d.ts.map +0 -1
  81. package/dist/plugins/lockVersion/index.js +0 -72
  82. package/dist/plugins/lockVersion/index.test.d.ts +0 -2
  83. package/dist/plugins/lockVersion/index.test.d.ts.map +0 -1
  84. package/dist/plugins/lockVersion/index.test.js +0 -129
  85. package/dist/plugins/lockVersion/schemas.d.ts +0 -10
  86. package/dist/plugins/lockVersion/schemas.d.ts.map +0 -1
  87. package/dist/plugins/lockVersion/schemas.js +0 -6
  88. package/src/plugins/lockVersion/index.test.ts +0 -176
  89. package/src/plugins/lockVersion/index.ts +0 -112
  90. package/src/plugins/lockVersion/schemas.ts +0 -9
package/dist/index.cjs CHANGED
@@ -1,8 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  var zod = require('zod');
4
- var fs = require('fs');
5
- var path = require('path');
6
4
  var promises = require('timers/promises');
7
5
 
8
6
  // src/types/properties.ts
@@ -654,16 +652,6 @@ function splitVersionedKey(versionedKey) {
654
652
  }
655
653
  return [versionedKey, void 0];
656
654
  }
657
- function normalizeImplementationToAppItem(implementation) {
658
- const [selectedApi, appVersion] = implementation.selected_api ? splitVersionedKey(implementation.selected_api) : [implementation.selected_api || "", void 0];
659
- return {
660
- title: implementation.name || selectedApi,
661
- key: selectedApi,
662
- current_implementation_id: implementation.selected_api || "",
663
- version: appVersion
664
- // Extract version separately
665
- };
666
- }
667
655
  function normalizeImplementationMetaToAppItem(implementationMeta) {
668
656
  const [selectedApi, appVersion] = splitVersionedKey(implementationMeta.id);
669
657
  return {
@@ -725,50 +713,8 @@ function normalizeActionItem(action) {
725
713
  type: "action"
726
714
  };
727
715
  }
728
- function groupVersionedAppKeysByType(appKeys) {
729
- const result = {
730
- selectedApi: [],
731
- slug: []
732
- };
733
- const seenSelectedApi = /* @__PURE__ */ new Set();
734
- const seenSlugs = /* @__PURE__ */ new Set();
735
- for (const key of appKeys) {
736
- const [keyWithoutVersion, version] = splitVersionedKey(key);
737
- const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
738
- if (uuidRegex.test(keyWithoutVersion)) {
739
- throw new Error(
740
- `UUID app keys are not supported. Use app slug or implementation ID instead of: ${key}`
741
- );
742
- }
743
- if (isSnakeCasedSlug(keyWithoutVersion)) {
744
- const dashified = dashifySnakeCasedSlug(keyWithoutVersion);
745
- const slugWithVersion = version ? `${dashified}@${version}` : dashified;
746
- if (!seenSlugs.has(slugWithVersion)) {
747
- seenSlugs.add(slugWithVersion);
748
- result.slug.push(slugWithVersion);
749
- }
750
- continue;
751
- }
752
- if (keyWithoutVersion.match(/^[a-z0-9]+(?:-[a-z0-9]+)*$/)) {
753
- seenSlugs.add(key);
754
- result.slug.push(key);
755
- continue;
756
- }
757
- if (!seenSelectedApi.has(key)) {
758
- seenSelectedApi.add(key);
759
- result.selectedApi.push(key);
760
- }
761
- }
762
- return result;
763
- }
764
- function groupAppKeysByType(appKeys) {
765
- const grouped = groupVersionedAppKeysByType(appKeys);
766
- return {
767
- selectedApi: [
768
- ...new Set(grouped.selectedApi.map((key) => key.split("@")[0]))
769
- ],
770
- slug: [...new Set(grouped.slug.map((key) => key.split("@")[0]))]
771
- };
716
+ function isSlug(slug) {
717
+ return !!slug.match(/^[a-z0-9]+(?:-[a-z0-9]+)*$/);
772
718
  }
773
719
  function isSnakeCasedSlug(slug) {
774
720
  if (slug.match(/^_[0-9]/)) {
@@ -785,15 +731,54 @@ function dashifySnakeCasedSlug(slug) {
785
731
  }
786
732
  return slug.replace(/_/g, "-");
787
733
  }
734
+ function isUuid(appKey) {
735
+ return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(
736
+ appKey
737
+ );
738
+ }
739
+ function toAppLocator(appKey) {
740
+ const [appKeyWithoutVersion, version] = splitVersionedKey(appKey);
741
+ if (isUuid(appKeyWithoutVersion)) {
742
+ throw new Error(
743
+ `UUID app keys are not supported. Use app slug or implementation ID instead of: ${appKey}`
744
+ );
745
+ }
746
+ const slug = isSlug(appKeyWithoutVersion) ? appKeyWithoutVersion : isSnakeCasedSlug(appKeyWithoutVersion) ? dashifySnakeCasedSlug(appKeyWithoutVersion) : void 0;
747
+ return {
748
+ lookupAppKey: appKeyWithoutVersion,
749
+ slug,
750
+ implementationName: slug ? void 0 : appKeyWithoutVersion,
751
+ version
752
+ };
753
+ }
754
+ function isResolvedAppLocator(appLocator) {
755
+ return !!appLocator.implementationName;
756
+ }
757
+ function toImplementationId(appLocator) {
758
+ return `${appLocator.implementationName}@${appLocator.version || "latest"}`;
759
+ }
788
760
 
789
761
  // src/plugins/listApps/index.ts
790
762
  var listAppsPlugin = ({ context }) => {
791
763
  const listApps = createPaginatedFunction(async function listAppsPage(options) {
792
764
  const api = context.api;
793
765
  const opts = options;
794
- const appKeys = [...opts.appKeys ?? []].map(
795
- (key) => splitVersionedKey(key)[0]
796
- );
766
+ const appLocators = await context.resolveAppKeys({
767
+ appKeys: [...opts.appKeys ?? []]
768
+ });
769
+ const implementationNameToLocator = {};
770
+ for (const locator of appLocators) {
771
+ implementationNameToLocator[locator.implementationName] = [
772
+ ...implementationNameToLocator[locator.implementationName] ?? [],
773
+ locator
774
+ ];
775
+ }
776
+ const duplicatedLookupAppKeys = Object.keys(implementationNameToLocator).filter((key) => implementationNameToLocator[key].length > 1).map((key) => implementationNameToLocator[key]).flat().map((locator) => locator.lookupAppKey);
777
+ if (duplicatedLookupAppKeys.length > 0) {
778
+ throw new Error(
779
+ `Duplicate lookup app keys found: ${duplicatedLookupAppKeys.join(", ")}`
780
+ );
781
+ }
797
782
  if (opts.search) {
798
783
  const searchParams2 = {};
799
784
  searchParams2.term = opts.search;
@@ -806,11 +791,17 @@ var listAppsPlugin = ({ context }) => {
806
791
  const implementations = searchEnvelope.results.map(
807
792
  normalizeImplementationMetaToAppItem
808
793
  );
809
- const appKeysSet = new Set(appKeys);
794
+ const implementationNameSet = new Set(
795
+ appLocators.map((locator) => locator.implementationName)
796
+ );
810
797
  for (const implementation of implementations) {
811
- if (!appKeysSet.has(implementation.key)) {
812
- appKeysSet.add(implementation.key);
813
- appKeys.push(implementation.key);
798
+ const [implementationName] = splitVersionedKey(implementation.key);
799
+ if (!implementationNameSet.has(implementationName)) {
800
+ implementationNameSet.add(implementationName);
801
+ appLocators.push({
802
+ ...toAppLocator(implementation.key),
803
+ implementationName
804
+ });
814
805
  }
815
806
  }
816
807
  }
@@ -818,19 +809,13 @@ var listAppsPlugin = ({ context }) => {
818
809
  if (opts.pageSize) {
819
810
  searchParams.limit = opts.pageSize.toString();
820
811
  }
821
- searchParams.latest_only = "true";
812
+ if (appLocators.length === 0) {
813
+ searchParams.latest_only = "true";
814
+ }
822
815
  if (opts.cursor) {
823
816
  searchParams.offset = opts.cursor;
824
817
  }
825
- if (appKeys.length > 0) {
826
- const groupedAppKeys = groupAppKeysByType(appKeys);
827
- if (groupedAppKeys.selectedApi.length > 0) {
828
- searchParams.selected_apis = groupedAppKeys.selectedApi.join(",");
829
- }
830
- if (groupedAppKeys.slug.length > 0) {
831
- searchParams.slugs = groupedAppKeys.slug.join(",");
832
- }
833
- }
818
+ searchParams.selected_apis = appLocators.map((locator) => toImplementationId(locator)).join(",");
834
819
  const implementationsEnvelope = await api.get(
835
820
  "/api/v4/implementations-meta/lookup/",
836
821
  {
@@ -1477,6 +1462,7 @@ var ListAuthenticationsSchema = zod.z.object({
1477
1462
  appKey: AppKeyPropertySchema.optional().describe(
1478
1463
  "App key of authentications to list (e.g., 'SlackCLIAPI')"
1479
1464
  ),
1465
+ authenticationIds: zod.z.array(zod.z.string()).optional().describe("List of authentication IDs to filter by"),
1480
1466
  search: zod.z.string().optional().describe("Search term to filter authentications by title"),
1481
1467
  title: zod.z.string().optional().describe("Filter authentications by exact title match"),
1482
1468
  accountId: zod.z.string().optional().describe("Filter by account ID"),
@@ -1500,6 +1486,9 @@ var listAuthenticationsPlugin = ({ context }) => {
1500
1486
  searchParams.versionless_selected_api = versionlessSelectedApi;
1501
1487
  }
1502
1488
  }
1489
+ if (options.authenticationIds && options.authenticationIds.length > 0) {
1490
+ searchParams.ids = options.authenticationIds.join(",");
1491
+ }
1503
1492
  if (options.search) {
1504
1493
  searchParams.search = options.search;
1505
1494
  } else if (options.title) {
@@ -1572,17 +1561,17 @@ var GetAppSchema = withOutputSchema(
1572
1561
  );
1573
1562
 
1574
1563
  // src/plugins/getApp/index.ts
1575
- var getAppPlugin = ({ context }) => {
1564
+ var getAppPlugin = ({ sdk }) => {
1576
1565
  const getApp = createFunction(async function getApp2(options) {
1577
- const app = await context.getImplementation(options.appKey);
1578
- if (!app) {
1579
- throw new ZapierAppNotFoundError("App not found", {
1580
- appKey: options.appKey
1581
- });
1566
+ const appsIterator = sdk.listApps({ appKeys: [options.appKey] }).items();
1567
+ for await (const app of appsIterator) {
1568
+ return {
1569
+ data: app
1570
+ };
1582
1571
  }
1583
- return {
1584
- data: app
1585
- };
1572
+ throw new ZapierAppNotFoundError("App not found", {
1573
+ appKey: options.appKey
1574
+ });
1586
1575
  }, GetAppSchema);
1587
1576
  return {
1588
1577
  getApp,
@@ -1948,6 +1937,67 @@ var requestPlugin = ({ context }) => {
1948
1937
  }
1949
1938
  };
1950
1939
  };
1940
+
1941
+ // src/utils/file-utils.ts
1942
+ var inMemoryFiles = {};
1943
+ var fsPromises = null;
1944
+ var pathModule = null;
1945
+ async function loadFsPromises() {
1946
+ if (fsPromises) return fsPromises;
1947
+ try {
1948
+ fsPromises = await import('fs/promises');
1949
+ return fsPromises;
1950
+ } catch {
1951
+ return null;
1952
+ }
1953
+ }
1954
+ async function loadPathModule() {
1955
+ if (pathModule) return pathModule;
1956
+ try {
1957
+ pathModule = await import('path');
1958
+ return pathModule;
1959
+ } catch {
1960
+ return null;
1961
+ }
1962
+ }
1963
+ async function resolve(path, basePath = "/") {
1964
+ const pathModule2 = await loadPathModule();
1965
+ if (pathModule2) {
1966
+ return pathModule2.resolve(path);
1967
+ }
1968
+ if (path.startsWith("/")) {
1969
+ return path;
1970
+ }
1971
+ if (path.startsWith("./")) {
1972
+ const cleanPath = path.slice(2);
1973
+ return basePath.endsWith("/") ? basePath + cleanPath : basePath + "/" + cleanPath;
1974
+ }
1975
+ if (path.startsWith("../")) {
1976
+ const cleanPath = path.replace(/^(\.\.\/)+/, "");
1977
+ return basePath.endsWith("/") ? basePath + cleanPath : basePath + "/" + cleanPath;
1978
+ }
1979
+ return basePath.endsWith("/") ? basePath + path : basePath + "/" + path;
1980
+ }
1981
+ async function writeFile(filePath, content) {
1982
+ const fs = await loadFsPromises();
1983
+ if (fs) {
1984
+ await fs.writeFile(filePath, content, "utf8");
1985
+ return;
1986
+ }
1987
+ inMemoryFiles[filePath] = content;
1988
+ }
1989
+ async function readFile(filePath) {
1990
+ const fs = await loadFsPromises();
1991
+ if (fs) {
1992
+ return await fs.readFile(filePath, "utf8");
1993
+ }
1994
+ const content = inMemoryFiles[filePath];
1995
+ if (content !== void 0) {
1996
+ return content;
1997
+ }
1998
+ throw new Error(`File not found: ${filePath}`);
1999
+ }
2000
+ var DEFAULT_CONFIG_PATH = ".zapierrc";
1951
2001
  var ManifestSchema = zod.z.object({
1952
2002
  apps: zod.z.record(
1953
2003
  zod.z.string(),
@@ -1970,6 +2020,15 @@ zod.z.object({
1970
2020
  ).optional().describe("Direct manifest object")
1971
2021
  });
1972
2022
 
2023
+ // src/utils/array-utils.ts
2024
+ async function toArrayFromAsync(asyncIterable) {
2025
+ const result = [];
2026
+ for await (const item of asyncIterable) {
2027
+ result.push(item);
2028
+ }
2029
+ return result;
2030
+ }
2031
+
1973
2032
  // src/plugins/manifest/index.ts
1974
2033
  function parseManifestContent(content, source) {
1975
2034
  try {
@@ -1987,169 +2046,235 @@ function parseManifestContent(content, source) {
1987
2046
  return null;
1988
2047
  }
1989
2048
  }
1990
- function loadManifestFromFile(filePath) {
2049
+ async function readManifestFromFile(filePath) {
1991
2050
  try {
1992
- const resolvedPath = path.resolve(filePath);
1993
- const content = fs.readFileSync(resolvedPath, "utf8");
2051
+ const resolvedPath = await resolve(filePath);
2052
+ const content = await readFile(resolvedPath);
1994
2053
  return parseManifestContent(content, resolvedPath);
1995
2054
  } catch {
1996
- console.warn(`\u26A0\uFE0F Failed to load manifest from ${filePath}`);
2055
+ console.warn(`\u26A0\uFE0F Failed to read manifest from ${filePath}`);
1997
2056
  return null;
1998
2057
  }
1999
2058
  }
2000
- var emitWarning = (appKey) => {
2001
- console.warn(
2002
- `
2003
- ${"\u26A0\uFE0F".padEnd(3)} ${"WARNING".padEnd(8)} No manifest version found for '${appKey}'`
2004
- );
2005
- console.warn(
2006
- ` ${"\u21B3".padEnd(3)} Using a manifest ensures version locking and prevents unexpected behavior due to version changes.`
2059
+ async function writeManifestToFile(manifest, filePath) {
2060
+ const resolvedPath = await resolve(filePath);
2061
+ await writeFile(resolvedPath, JSON.stringify(manifest, null, 2));
2062
+ }
2063
+ async function getPreferredManifestEntryKey({
2064
+ appKey,
2065
+ api
2066
+ }) {
2067
+ const locator = toAppLocator(appKey);
2068
+ if (locator.slug) {
2069
+ return locator.slug;
2070
+ }
2071
+ if (locator.implementationName) {
2072
+ try {
2073
+ const implementationsEnvelope = await api.get(`/api/v4/implementations-meta/lookup/`, {
2074
+ searchParams: {
2075
+ selected_apis: locator.implementationName
2076
+ }
2077
+ });
2078
+ if (implementationsEnvelope.results.length > 0 && implementationsEnvelope.results[0].slug) {
2079
+ return implementationsEnvelope.results[0].slug;
2080
+ }
2081
+ } catch {
2082
+ }
2083
+ return locator.implementationName;
2084
+ }
2085
+ return locator.lookupAppKey;
2086
+ }
2087
+ async function listAppsForSlugsPage({
2088
+ slugs,
2089
+ cursor,
2090
+ api
2091
+ }) {
2092
+ const searchParams = {};
2093
+ if (slugs.length > 0) {
2094
+ searchParams.slugs = slugs.join(",");
2095
+ }
2096
+ if (cursor) {
2097
+ searchParams.offset = cursor;
2098
+ }
2099
+ const implementationsEnvelope = await api.get(
2100
+ "/api/v4/implementations-meta/lookup/",
2101
+ {
2102
+ searchParams
2103
+ }
2007
2104
  );
2008
- console.warn(
2009
- ` ${"\u21B3".padEnd(3)} Generate/update the manifest with: \`zapier-sdk lock-version ${appKey}\`
2010
- `
2105
+ return {
2106
+ data: implementationsEnvelope.results.map(
2107
+ normalizeImplementationMetaToAppItem
2108
+ ),
2109
+ nextCursor: extractCursor(implementationsEnvelope)
2110
+ };
2111
+ }
2112
+ function findManifestEntry({
2113
+ appKey,
2114
+ manifest
2115
+ }) {
2116
+ const [appKeyWithoutVersion] = splitVersionedKey(appKey);
2117
+ if (manifest.apps[appKeyWithoutVersion]) {
2118
+ return [appKeyWithoutVersion, manifest.apps[appKeyWithoutVersion]];
2119
+ }
2120
+ if (isSnakeCasedSlug(appKey)) {
2121
+ const slug = dashifySnakeCasedSlug(appKey);
2122
+ if (manifest.apps[slug]) {
2123
+ return [slug, manifest.apps[slug]];
2124
+ }
2125
+ }
2126
+ for (const [key, entry] of Object.entries(manifest.apps)) {
2127
+ if (entry.implementationName === appKeyWithoutVersion) {
2128
+ return [key, entry];
2129
+ }
2130
+ }
2131
+ return null;
2132
+ }
2133
+ async function resolveAppKeys({
2134
+ appKeys,
2135
+ api,
2136
+ manifest
2137
+ }) {
2138
+ const locators = appKeys.map(toAppLocator);
2139
+ const locatorsWithManifest = locators.map((locator) => {
2140
+ const manifestEntryResult = findManifestEntry({
2141
+ appKey: locator.lookupAppKey,
2142
+ manifest
2143
+ });
2144
+ if (manifestEntryResult) {
2145
+ const [, manifestEntry] = manifestEntryResult;
2146
+ const resolvedVersion = locator.version || manifestEntry.version;
2147
+ const resolvedLocator = {
2148
+ ...locator,
2149
+ implementationName: manifestEntry.implementationName,
2150
+ version: resolvedVersion
2151
+ };
2152
+ return resolvedLocator;
2153
+ }
2154
+ return locator;
2155
+ });
2156
+ const unresolvedLocators = locatorsWithManifest.filter(
2157
+ (locator) => !isResolvedAppLocator(locator)
2011
2158
  );
2012
- };
2159
+ const slugsToResolve = unresolvedLocators.map((locator) => locator.slug).filter((slug) => !!slug).filter((slug, index, array) => array.indexOf(slug) === index);
2160
+ if (slugsToResolve.length === 0) {
2161
+ return locatorsWithManifest.filter(isResolvedAppLocator);
2162
+ }
2163
+ const iterator = paginate(listAppsForSlugsPage, {
2164
+ slugs: slugsToResolve,
2165
+ api
2166
+ });
2167
+ const pages = await toArrayFromAsync(iterator);
2168
+ const apps = pages.flatMap((page) => page.data);
2169
+ const slugToAppData = /* @__PURE__ */ new Map();
2170
+ for (const app of apps) {
2171
+ if (app.slug) {
2172
+ slugToAppData.set(app.slug, app);
2173
+ }
2174
+ }
2175
+ const slugResolvedLocators = locatorsWithManifest.map((locator) => {
2176
+ if (isResolvedAppLocator(locator)) {
2177
+ return locator;
2178
+ }
2179
+ if (locator.slug) {
2180
+ const appData = slugToAppData.get(locator.slug);
2181
+ if (appData) {
2182
+ const manifestEntryByImplementationName = findManifestEntry({
2183
+ appKey: appData.key,
2184
+ // appData.key is the implementation name
2185
+ manifest
2186
+ });
2187
+ let version = locator.version;
2188
+ if (!version) {
2189
+ version = manifestEntryByImplementationName ? manifestEntryByImplementationName[1].version || appData.version : appData.version;
2190
+ }
2191
+ return {
2192
+ ...locator,
2193
+ implementationName: appData.key,
2194
+ version
2195
+ };
2196
+ }
2197
+ }
2198
+ return locator;
2199
+ });
2200
+ return slugResolvedLocators.filter(isResolvedAppLocator);
2201
+ }
2013
2202
  var manifestPlugin = (params) => {
2014
- const { sdk, context } = params;
2203
+ const { context } = params;
2015
2204
  const { api, options } = context;
2016
- const { manifestPath = ".zapierrc", manifest } = options || {};
2205
+ const { manifestPath = DEFAULT_CONFIG_PATH, manifest } = options || {};
2017
2206
  let resolvedManifest;
2018
- function resolveManifest() {
2207
+ async function resolveManifest() {
2019
2208
  if (manifest) {
2020
2209
  return manifest;
2021
2210
  }
2022
2211
  if (manifestPath) {
2023
- return loadManifestFromFile(manifestPath);
2212
+ return await readManifestFromFile(manifestPath);
2024
2213
  }
2025
2214
  return null;
2026
2215
  }
2027
- const getResolvedManifest = () => {
2216
+ const getResolvedManifest = async () => {
2028
2217
  if (typeof resolvedManifest === "undefined") {
2029
- resolvedManifest = resolveManifest() ?? null;
2218
+ resolvedManifest = await resolveManifest() ?? null;
2030
2219
  }
2031
2220
  return resolvedManifest;
2032
2221
  };
2033
- const getManifestEntry = (appKey) => {
2034
- return getResolvedManifest()?.apps?.[appKey] || null;
2222
+ const getVersionedImplementationId = async (appKey) => {
2223
+ const resolvedApps = await resolveAppKeys({
2224
+ appKeys: [appKey],
2225
+ api,
2226
+ manifest: await getResolvedManifest() ?? { apps: {} }
2227
+ });
2228
+ const resolvedApp = resolvedApps[0];
2229
+ if (!resolvedApp) return null;
2230
+ return `${resolvedApp.implementationName}@${resolvedApp.version || "latest"}`;
2035
2231
  };
2036
- const getImplementation = async (appKey) => {
2037
- let selectedApi = null;
2038
- const manifestImplementation = getResolvedManifest()?.apps?.[appKey];
2039
- const [versionlessAppKey, version] = splitVersionedKey(appKey);
2040
- if (version) {
2041
- selectedApi = `${versionlessAppKey}@${version}`;
2042
- } else if (manifestImplementation) {
2043
- selectedApi = `${manifestImplementation.implementationName}@${manifestImplementation.version || "latest"}`;
2044
- }
2045
- if (selectedApi) {
2046
- const searchParams = {
2047
- selected_apis: selectedApi
2048
- };
2049
- const implementationData = await api.get(
2050
- "/api/v4/implementations/",
2051
- {
2052
- searchParams
2232
+ const updateManifestEntry = async (appKey, entry, configPath = DEFAULT_CONFIG_PATH) => {
2233
+ const manifest2 = await readManifestFromFile(configPath) || { apps: {} };
2234
+ let existingEntry = findManifestEntry({
2235
+ appKey,
2236
+ manifest: manifest2
2237
+ });
2238
+ if (!existingEntry) {
2239
+ try {
2240
+ const resolvedApps = await resolveAppKeys({
2241
+ appKeys: [appKey],
2242
+ api,
2243
+ manifest: manifest2
2244
+ });
2245
+ if (resolvedApps.length > 0) {
2246
+ const resolvedImplementationName = resolvedApps[0].implementationName;
2247
+ existingEntry = findManifestEntry({
2248
+ appKey: resolvedImplementationName,
2249
+ manifest: manifest2
2250
+ });
2053
2251
  }
2054
- );
2055
- const implementationResults = implementationData.results[0];
2056
- if (!implementationResults) return null;
2057
- return normalizeImplementationToAppItem(implementationResults);
2058
- }
2059
- emitWarning(appKey);
2060
- const appsIterator = sdk.listApps({ appKeys: [appKey] }).items();
2061
- const apps = [];
2062
- for await (const app2 of appsIterator) {
2063
- apps.push(app2);
2064
- break;
2065
- }
2066
- if (apps.length === 0) {
2067
- return null;
2068
- }
2069
- const app = apps[0];
2070
- return app;
2071
- };
2072
- const getVersionedImplementationId = async (appKey) => {
2073
- const manifestEntry = getManifestEntry(appKey);
2074
- if (manifestEntry) {
2075
- return `${manifestEntry.implementationName}@${manifestEntry.version || "latest"}`;
2252
+ } catch {
2253
+ }
2076
2254
  }
2077
- const implementation = await getImplementation(appKey);
2078
- if (!implementation) return null;
2079
- return implementation.current_implementation_id;
2080
- };
2081
- return {
2082
- context: {
2083
- getVersionedImplementationId,
2084
- getManifestEntry,
2085
- getImplementation
2255
+ let manifestKey;
2256
+ if (existingEntry) {
2257
+ manifestKey = existingEntry[0];
2258
+ } else {
2259
+ manifestKey = await getPreferredManifestEntryKey({
2260
+ appKey,
2261
+ api
2262
+ });
2086
2263
  }
2264
+ manifest2.apps[manifestKey] = entry;
2265
+ await writeManifestToFile(manifest2, configPath);
2266
+ resolvedManifest = void 0;
2267
+ return [manifestKey, entry];
2087
2268
  };
2088
- };
2089
- var LockVersionSchema = zod.z.object({
2090
- appKey: zod.z.string().describe("The app key to lock version for (e.g., 'slack', 'gmail')")
2091
- });
2092
- var lockVersionPlugin = ({ sdk }) => {
2093
- const lockVersion = createFunction(
2094
- async function lockVersion2(options) {
2095
- const { appKey, configPath = ".zapierrc" } = options;
2096
- const resolvedPath = path.resolve(configPath);
2097
- const appsIterator = sdk.listApps({ appKeys: [appKey] }).items();
2098
- const apps = [];
2099
- for await (const app2 of appsIterator) {
2100
- apps.push(app2);
2101
- break;
2102
- }
2103
- const app = apps[0];
2104
- const currentImplementationId = app.current_implementation_id;
2105
- const [implementationName, version] = currentImplementationId.split("@");
2106
- if (!implementationName || !version) {
2107
- throw new Error(
2108
- `Invalid implementation ID format: ${currentImplementationId}. Expected format: <implementationName>@<version>`
2109
- );
2110
- }
2111
- let config = { apps: {} };
2112
- if (fs.existsSync(resolvedPath)) {
2113
- try {
2114
- const configContent = fs.readFileSync(resolvedPath, "utf8");
2115
- config = JSON.parse(configContent);
2116
- if (!config.apps) {
2117
- config.apps = {};
2118
- }
2119
- } catch (error) {
2120
- console.warn(
2121
- `\u26A0\uFE0F Failed to parse existing config file, creating new one: ${error}`
2122
- );
2123
- config = { apps: {} };
2124
- }
2125
- }
2126
- config.apps[appKey] = {
2127
- implementationName,
2128
- version
2129
- };
2130
- fs.writeFileSync(resolvedPath, JSON.stringify(config, null, 2));
2131
- return {
2132
- data: {
2133
- ...app,
2134
- implementationName,
2135
- version
2136
- },
2137
- configPath: resolvedPath
2138
- };
2139
- },
2140
- LockVersionSchema.extend({
2141
- configPath: zod.z.string().optional().describe("Path to .zapierrc file (defaults to '.zapierrc')")
2142
- })
2143
- );
2144
2269
  return {
2145
- lockVersion,
2146
2270
  context: {
2147
- meta: {
2148
- lockVersion: {
2149
- categories: ["utility"],
2150
- inputSchema: LockVersionSchema
2151
- }
2152
- }
2271
+ getVersionedImplementationId,
2272
+ resolveAppKeys: async ({ appKeys }) => resolveAppKeys({
2273
+ appKeys,
2274
+ api,
2275
+ manifest: await getResolvedManifest() ?? { apps: {} }
2276
+ }),
2277
+ updateManifestEntry
2153
2278
  }
2154
2279
  };
2155
2280
  };
@@ -3197,7 +3322,7 @@ function createSdk(options = {}, initialSdk = {}, initialContext = { meta: {} })
3197
3322
  };
3198
3323
  }
3199
3324
  function createZapierSdkWithoutRegistry(options = {}) {
3200
- return createSdk(options).addPlugin(apiPlugin).addPlugin(listAppsPlugin).addPlugin(manifestPlugin).addPlugin(getAppPlugin).addPlugin(listActionsPlugin).addPlugin(getActionPlugin).addPlugin(listInputFieldsPlugin).addPlugin(listInputFieldChoicesPlugin).addPlugin(runActionPlugin).addPlugin(lockVersionPlugin).addPlugin(listAuthenticationsPlugin).addPlugin(getAuthenticationPlugin).addPlugin(findFirstAuthenticationPlugin).addPlugin(findUniqueAuthenticationPlugin).addPlugin(requestPlugin).addPlugin(fetchPlugin).addPlugin(appsPlugin).addPlugin(getProfilePlugin);
3325
+ return createSdk(options).addPlugin(apiPlugin).addPlugin(manifestPlugin).addPlugin(listAppsPlugin).addPlugin(getAppPlugin).addPlugin(listActionsPlugin).addPlugin(getActionPlugin).addPlugin(listInputFieldsPlugin).addPlugin(listInputFieldChoicesPlugin).addPlugin(runActionPlugin).addPlugin(listAuthenticationsPlugin).addPlugin(getAuthenticationPlugin).addPlugin(findFirstAuthenticationPlugin).addPlugin(findUniqueAuthenticationPlugin).addPlugin(requestPlugin).addPlugin(fetchPlugin).addPlugin(appsPlugin).addPlugin(getProfilePlugin);
3201
3326
  }
3202
3327
  function createZapierSdk(options = {}) {
3203
3328
  return createZapierSdkWithoutRegistry(options).addPlugin(registryPlugin);
@@ -3207,6 +3332,7 @@ exports.ActionKeyPropertySchema = ActionKeyPropertySchema;
3207
3332
  exports.ActionTypePropertySchema = ActionTypePropertySchema;
3208
3333
  exports.AppKeyPropertySchema = AppKeyPropertySchema;
3209
3334
  exports.AuthenticationIdPropertySchema = AuthenticationIdPropertySchema;
3335
+ exports.DEFAULT_CONFIG_PATH = DEFAULT_CONFIG_PATH;
3210
3336
  exports.DebugPropertySchema = DebugPropertySchema;
3211
3337
  exports.InputsPropertySchema = InputsPropertySchema;
3212
3338
  exports.LimitPropertySchema = LimitPropertySchema;
@@ -3239,11 +3365,13 @@ exports.createZapierSdk = createZapierSdk;
3239
3365
  exports.createZapierSdkWithoutRegistry = createZapierSdkWithoutRegistry;
3240
3366
  exports.fetchPlugin = fetchPlugin;
3241
3367
  exports.findFirstAuthenticationPlugin = findFirstAuthenticationPlugin;
3368
+ exports.findManifestEntry = findManifestEntry;
3242
3369
  exports.findUniqueAuthenticationPlugin = findUniqueAuthenticationPlugin;
3243
3370
  exports.formatErrorMessage = formatErrorMessage;
3244
3371
  exports.getActionPlugin = getActionPlugin;
3245
3372
  exports.getAppPlugin = getAppPlugin;
3246
3373
  exports.getAuthenticationPlugin = getAuthenticationPlugin;
3374
+ exports.getPreferredManifestEntryKey = getPreferredManifestEntryKey;
3247
3375
  exports.getProfilePlugin = getProfilePlugin;
3248
3376
  exports.getResolutionOrder = getResolutionOrder;
3249
3377
  exports.getResolutionOrderForParams = getResolutionOrderForParams;
@@ -3260,9 +3388,8 @@ exports.listActionsPlugin = listActionsPlugin;
3260
3388
  exports.listAppsPlugin = listAppsPlugin;
3261
3389
  exports.listAuthenticationsPlugin = listAuthenticationsPlugin;
3262
3390
  exports.listInputFieldsPlugin = listInputFieldsPlugin;
3263
- exports.loadManifestFromFile = loadManifestFromFile;
3264
- exports.lockVersionPlugin = lockVersionPlugin;
3265
3391
  exports.manifestPlugin = manifestPlugin;
3392
+ exports.readManifestFromFile = readManifestFromFile;
3266
3393
  exports.registryPlugin = registryPlugin;
3267
3394
  exports.requestPlugin = requestPlugin;
3268
3395
  exports.resolverRegistry = resolverRegistry;