Repository: hasura/graphql-engine
File: console/src/telemetryFilter.ts (TypeScript)

Code before:

const filterPayloadAllowList: unknown[] = [];

const DATA_PATH = '/data';
const API_EXPLORER_PATH = '/api-explorer';
const REMOTE_SCHEMAS_PATH = '/remote-schemas';
const EVENTS_PATH = '/events';

const dataHandler = (path: string): string => {
  return (
    DATA_PATH +
    path
      .replace(/\/schema\/([^/]*)(\/)?/, '/schema/SCHEMA_NAME$2')
      .replace(
        /(\/schema\/.*)\/tables\/([^/]*)(\/.*)?/,
        '$1/tables/TABLE_NAME$3'
      )
      .replace(/(\/schema\/.*)\/views\/([^/]*)(\/.*)?/, '$1/views/VIEW_NAME$3')
      .replace(
        /(\/schema\/.*)\/functions\/([^/]*)(\/.*)?/,
        '$1/functions/FUNCTION_NAME$3'
      )
  );
};

const apiExplorerHandler = (): string => {
  return API_EXPLORER_PATH;
};

const remoteSchemasHandler = (path: string): string => {
  return (
    REMOTE_SCHEMAS_PATH +
    path.replace(/(\/manage\/)[^/]*(\/\w+.*)$/, '$1REMOTE_SCHEMA_NAME$2')
  );
};

const eventsHandler = (path: string): string => {
  return (
    EVENTS_PATH +
    path.replace(/(\/manage\/triggers\/)[^/]*(\/\w+.*)$/, '$1TRIGGER_NAME$2')
  );
};

const sanitiseUrl = (path: string): string => {
  path = path.replace(new RegExp(globals.urlPrefix, 'g'), '');
  if (path.indexOf(DATA_PATH) === 0) {
    return dataHandler(path.slice(DATA_PATH.length));
  }
  if (path.indexOf(API_EXPLORER_PATH) === 0) {
    return apiExplorerHandler();
  }

Review comment (suggestion):
For this string constant, we could do:
```ts
const API_EXPLORER_PATH = '/api-explorer' as const;
```
and omit providing return types for the function, so that we would have following functions signature: 

<img width="495" alt="Screenshot 2020-05-04 at 23 46 07" src="https://user-images.githubusercontent.com/9019397/81016968-bc479080-8e61-11ea-8573-f03b02513642.png">


Code after:

const DATA_PATH = '/data' as const;
const API_EXPLORER_PATH = '/api-explorer' as const;
const REMOTE_SCHEMAS_PATH = '/remote-schemas' as const;
const EVENTS_PATH = '/events' as const;

const dataHandler = (path: string) => {
  return (
    DATA_PATH +
    path
      .replace(/\/schema\/([^/]*)(\/)?/, '/schema/SCHEMA_NAME$2')
      .replace(
        /(\/schema\/.*)\/tables\/([^/]*)(\/.*)?/,
        '$1/tables/TABLE_NAME$3'
      )
      .replace(/(\/schema\/.*)\/views\/([^/]*)(\/.*)?/, '$1/views/VIEW_NAME$3')
      .replace(
        /(\/schema\/.*)\/functions\/([^/]*)(\/.*)?/,
        '$1/functions/FUNCTION_NAME$3'
      )
  );
};

const apiExplorerHandler = () => {
  return API_EXPLORER_PATH;
};

const remoteSchemasHandler = (path: string) => {
  return (
    REMOTE_SCHEMAS_PATH +
    path.replace(/(\/manage\/)[^/]*(\/\w+.*)$/, '$1REMOTE_SCHEMA_NAME$2')
  );
};

const eventsHandler = (path: string) => {
  return (
    EVENTS_PATH +
    path.replace(/(\/manage\/triggers\/)[^/]*(\/\w+.*)$/, '$1TRIGGER_NAME$2')
  );
};

const sanitiseUrl = (path: string) => {
  const newPath = path.replace(new RegExp(globals.urlPrefix, 'g'), '');
  if (newPath.indexOf(DATA_PATH) === 0) {
    return dataHandler(newPath.slice(DATA_PATH.length));
  }
  if (newPath.indexOf(API_EXPLORER_PATH) === 0) {
    return apiExplorerHandler();
  }
  if (newPath.indexOf(REMOTE_SCHEMAS_PATH) === 0) {
    return remoteSchemasHandler(newPath.slice(REMOTE_SCHEMAS_PATH.length));