@cronvello/sdk 0.3.0 → 0.4.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.
package/dist/index.js CHANGED
@@ -1914,6 +1914,112 @@ function field(value, min, max, label) {
1914
1914
  return value;
1915
1915
  }
1916
1916
 
1917
+ // src/client/admin-client.ts
1918
+ var CronvelloAdminClient = class {
1919
+ transport;
1920
+ /** The resolved base URL in use. */
1921
+ baseUrl;
1922
+ /** Registration, status, key rotation and removal of external apps. */
1923
+ externalApps;
1924
+ constructor(options) {
1925
+ if (!options || !options.serviceKey) {
1926
+ throw new CronvelloConfigError(
1927
+ "CronvelloAdminClient requires a `serviceKey` (Cronvello's backend-to-backend key, not an account apiKey)."
1928
+ );
1929
+ }
1930
+ this.baseUrl = (options.baseUrl ?? CRONVELLO_DEFAULT_BASE_URL).replace(/\/+$/, "");
1931
+ this.transport = new Transport({
1932
+ baseUrl: this.baseUrl,
1933
+ apiKey: options.serviceKey,
1934
+ ...options.timeoutMs !== void 0 ? { timeoutMs: options.timeoutMs } : {},
1935
+ ...options.maxRetries !== void 0 ? { maxRetries: options.maxRetries } : {},
1936
+ ...options.fetch ? { fetch: options.fetch } : {},
1937
+ ...options.onRequest ? { onRequest: options.onRequest } : {}
1938
+ });
1939
+ this.externalApps = new ExternalAppsResource(this.transport);
1940
+ }
1941
+ };
1942
+ var ExternalAppsResource = class {
1943
+ constructor(t) {
1944
+ this.t = t;
1945
+ }
1946
+ t;
1947
+ /**
1948
+ * Idempotent upsert, keyed on the string `appId`: creates when unknown, updates credentials
1949
+ * and URL when it already exists. Safe to retry and to re-run on every provisioning pass.
1950
+ *
1951
+ * When the server mints the token (`generateApiKey: true` on a *new* app) it comes back as
1952
+ * `generatedApiKey` — plaintext, exactly once. On an update it is `null`.
1953
+ */
1954
+ register(input) {
1955
+ assertRegisterInput(input);
1956
+ return this.t.request({ method: "POST", path: "/external-apps/service/register", body: input });
1957
+ }
1958
+ /** Registration status, last-sync info and job count for one app, by its string `appId`. */
1959
+ status(appId) {
1960
+ assertAppId(appId, "status");
1961
+ return this.t.request({ method: "GET", path: `/external-apps/service/status/${enc2(appId)}` });
1962
+ }
1963
+ /**
1964
+ * Mint a fresh per-app token, by string `appId`. The new token is returned once as `newApiKey`
1965
+ * and must be written into the app's environment — the previous one stops working.
1966
+ *
1967
+ * Use this for drift recovery when the current token is no longer known.
1968
+ */
1969
+ rotateKey(appId) {
1970
+ assertAppId(appId, "rotateKey");
1971
+ return this.t.request({ method: "POST", path: `/external-apps/service/rotate-key/${enc2(appId)}` });
1972
+ }
1973
+ /**
1974
+ * Delete a registration, cascading its jobs and tasks.
1975
+ *
1976
+ * ⚠ This one takes the **numeric** {@link ExternalApp.id}, not the string `appId` the other
1977
+ * three methods take — an asymmetry in the server's route contract. Read the id off a
1978
+ * `register()` result (or a prior lookup); passing a string `appId` here is rejected locally.
1979
+ */
1980
+ delete(id) {
1981
+ if (!Number.isInteger(id) || id <= 0) {
1982
+ throw new CronvelloConfigError(
1983
+ `externalApps.delete() takes the numeric app id (ExternalApp.id), not the string appId \u2014 received ${JSON.stringify(id)}.`
1984
+ );
1985
+ }
1986
+ return this.t.request({ method: "DELETE", path: `/external-apps/service/delete/${id}` });
1987
+ }
1988
+ };
1989
+ function assertRegisterInput(input) {
1990
+ if (!input || typeof input !== "object") {
1991
+ throw new CronvelloConfigError("externalApps.register() requires an input object.");
1992
+ }
1993
+ if (!input.appId || !input.appId.trim()) {
1994
+ throw new CronvelloConfigError("externalApps.register() requires a non-empty `appId`.");
1995
+ }
1996
+ if (!input.name || !input.name.trim()) {
1997
+ throw new CronvelloConfigError("externalApps.register() requires a non-empty `name`.");
1998
+ }
1999
+ if (!input.base_url && input.targetUrl === void 0) {
2000
+ throw new CronvelloConfigError("externalApps.register() requires either `base_url` or `targetUrl`.");
2001
+ }
2002
+ const authMethod = input.authMethod ?? (input.oauthClientId || input.oauthClientSecret ? "oauth" : "api_key");
2003
+ if (authMethod === "api_key" && !input.apiKey && !input.generateApiKey) {
2004
+ throw new CronvelloConfigError(
2005
+ "externalApps.register() with api_key auth requires either `apiKey` or `generateApiKey: true`."
2006
+ );
2007
+ }
2008
+ if (authMethod === "oauth" && (!input.oauthClientId || !input.oauthClientSecret)) {
2009
+ throw new CronvelloConfigError(
2010
+ "externalApps.register() with oauth auth requires both `oauthClientId` and `oauthClientSecret`."
2011
+ );
2012
+ }
2013
+ }
2014
+ function assertAppId(appId, method) {
2015
+ if (typeof appId !== "string" || !appId.trim()) {
2016
+ throw new CronvelloConfigError(`externalApps.${method}() requires a non-empty string appId.`);
2017
+ }
2018
+ }
2019
+ function enc2(segment) {
2020
+ return encodeURIComponent(segment);
2021
+ }
2022
+
1917
2023
  // src/index.ts
1918
2024
  function generateDispatchSecret(bytes = 32) {
1919
2025
  const buf = new Uint8Array(bytes);
@@ -1921,6 +2027,6 @@ function generateDispatchSecret(bytes = 32) {
1921
2027
  return Array.from(buf).map((b) => b.toString(16).padStart(2, "0")).join("");
1922
2028
  }
1923
2029
 
1924
- export { CRONVELLO_DEFAULT_BASE_URL, CronvelloApiError, CronvelloClient, CronvelloConfigError, CronvelloError, CronvelloNetworkError, cron, daily, defineCronvello, every, everyHours, everyMinutes, formatSyncResult, generateDispatchSecret, hourly, isValidTimeZone, monthly, nextOccurrence, previewSchedule, schedule, validateCron, weekdays, weekends, weekly };
2030
+ export { CRONVELLO_DEFAULT_BASE_URL, CronvelloAdminClient, CronvelloApiError, CronvelloClient, CronvelloConfigError, CronvelloError, CronvelloNetworkError, cron, daily, defineCronvello, every, everyHours, everyMinutes, formatSyncResult, generateDispatchSecret, hourly, isValidTimeZone, monthly, nextOccurrence, previewSchedule, schedule, validateCron, weekdays, weekends, weekly };
1925
2031
  //# sourceMappingURL=index.js.map
1926
2032
  //# sourceMappingURL=index.js.map