Repository: logto-io/logto
File: packages/core/src/libraries/jwt-customizer.ts (TypeScript)

Code before:
       * worker service. A production script will be deployed to `/api/custom-jwt`
       * endpoint and a test script will be deployed to `/api/custom-jwt/test` endpoint.
       *
       * If the current use case is `test`, then the script should be deployed to a `/test` endpoint;
       * otherwise, the script should be deployed to the `/api/custom-jwt` endpoint and overwrite
       * previous handler of the API endpoint.
       */
      [payload.key]: { [payload.useCase]: payload.value.script },
    };

    await client.put(`/api/services/custom-jwt/worker`, {
      body: deepmerge(customizerScriptsFromDatabase, newCustomizerScripts),
    });
  }

  async undeployJwtCustomizerScript<T extends LogtoJwtTokenKey>(consoleLog: ConsoleLog, key: T) {
    if (!EnvSet.values.isCloud) {
      consoleLog.warn(
        'Early terminate `undeployJwtCustomizerScript` since we do not deploy the script to dedicated computing resource for OSS version.'
      );
      return;
    }

    if (this.isRegionalAzureFunctionAppConfigured) {
      consoleLog.info(
        'Skipping Cloudflare Workers deployment since regional Azure Function App is configured.'
      );
      return;
    }

    const [client, jwtCustomizers] = await Promise.all([
      this.cloudConnection.getClient(),
      this.logtoConfigs.getJwtCustomizers(consoleLog),
    ]);

    assert(jwtCustomizers[key], new RequestError({ code: 'entity.not_exists', name: key }));

    // Undeploy the worker directly if the only JWT customizer is being deleted.
    if (Object.entries(jwtCustomizers).length === 1) {
      await client.delete(`/api/services/custom-jwt/worker`);
      return;
    }

    // Remove the JWT customizer script (of given `key`) from the existing JWT customizer scripts and redeploy.
    const customizerScriptsFromDatabase = getJwtCustomizerScripts(jwtCustomizers);
    const newCustomizerScripts: CustomJwtDeployRequestBody = {
      [key]: {
        production: undefined,
        test: undefined,
      },
    };

Review comment (suggestion):
```suggestion
        'Skipping Cloudflare Workers undeployment since regional Azure Function App is configured.'
```

Code after:
       * worker service. A production script will be deployed to `/api/custom-jwt`
       * endpoint and a test script will be deployed to `/api/custom-jwt/test` endpoint.
       *
       * If the current use case is `test`, then the script should be deployed to a `/test` endpoint;
       * otherwise, the script should be deployed to the `/api/custom-jwt` endpoint and overwrite
       * previous handler of the API endpoint.
       */
      [payload.key]: { [payload.useCase]: payload.value.script },
    };

    await client.put(`/api/services/custom-jwt/worker`, {
      body: deepmerge(customizerScriptsFromDatabase, newCustomizerScripts),
    });
  }

  async undeployJwtCustomizerScript<T extends LogtoJwtTokenKey>(consoleLog: ConsoleLog, key: T) {
    if (!EnvSet.values.isCloud) {
      consoleLog.warn(
        'Early terminate `undeployJwtCustomizerScript` since we do not deploy the script to dedicated computing resource for OSS version.'
      );
      return;
    }

    if (this.isRegionalAzureFunctionAppConfigured) {
      consoleLog.info(
        'Skipping Cloudflare Workers undeployment since regional Azure Function App is configured.'
      );
      return;
    }

    const [client, jwtCustomizers] = await Promise.all([
      this.cloudConnection.getClient(),
      this.logtoConfigs.getJwtCustomizers(consoleLog),
    ]);

    assert(jwtCustomizers[key], new RequestError({ code: 'entity.not_exists', name: key }));

    // Undeploy the worker directly if the only JWT customizer is being deleted.
    if (Object.entries(jwtCustomizers).length === 1) {
      await client.delete(`/api/services/custom-jwt/worker`);
      return;
    }

    // Remove the JWT customizer script (of given `key`) from the existing JWT customizer scripts and redeploy.
    const customizerScriptsFromDatabase = getJwtCustomizerScripts(jwtCustomizers);
    const newCustomizerScripts: CustomJwtDeployRequestBody = {
      [key]: {
        production: undefined,
        test: undefined,
      },
    };