Skip to content

Refactor use of util.format #35

@adam-cyclones

Description

@adam-cyclones

util.format

Supports %s C style replacement but JavaScript itself supports template literals which are clearer to see the expected output and don't need an import.
A function can be used for composure.

const journeyURLTemplate =
  '%s/json%s/realm-config/authentication/authenticationtrees/trees/%s';
const nodeURLTemplate =
  '%s/json%s/realm-config/authentication/authenticationtrees/nodes/%s/%s';
  
// five minutes later
const urlString = util.format(
  queryAllTreesURLTemplate,
  storage.session.getTenant(),
  getCurrentRealmPath()
);

const response = await generateAmApi(getTreeApiConfig())
    .get(urlString, {
      withCredentials: true,
    })

Becomes

const journeyURLTemplate = ({ host, treename }) => `${host}/json%s/realm-config/authentication/authenticationtrees/trees/${treename}`;
const nodeURLTemplate = ({host, something, somethingElse}) => `${host}/json%s/realm-config/authentication/authenticationtrees/nodes/${something}/${somethingElse}`;

const response = await generateAmApi(getTreeApiConfig())
    .get(journeyURLTemplate({ host, treename }), {
      withCredentials: true,
    })

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions