The Uniform redirect file converter
The redirect file converter is a Node.js script designed to help export redirects into a file format destined for other platforms to perform the redirects.
ENV#
The recommended approach is to use a .env
file with the following properties:
UNIFORM_API_KEY=<YOUR API KEY>
UNIFORM_PROJECT_ID=<YOUR PROJECT ID>
Usage#
Import
import { RedirectFileConverter } from '@uniformdev/redirect'
This contains a function used to convert Uniform redirects to another format.
RedirectFileConverter({
client, // Redirect SDK client, automatically provisioned if using the env file from above
redirectEntryObject, // Function to convert a Uniform redirect object into your desired format
wildcardConverter, // Function to convert the format of wildcards from the Uniform format :wildcard to the desired format
writeFile, // Function that writes out to a file, or processes the resulting converted output
})
redirectEntryObject#
Convert a Uniform defined redirect into another format
Inputs
- redirect: singular redirect object
Outputs
- object representing the new format for the redirect
const redirectEntryObject = (r) => ({
source: r.redirect.sourceUrl,
destination: r.redirect.targetUrl,
permanent: r.redirect.targetStatusCode === 301,
})
wildcardConverter#
Different platforms require different formats for wildcards, use this converter to adjust your wildcards. Here is an example for converting Uniform wildcards /:wildcard
-> /:wildcard
to those used by Cloudflare /*
-> /$1
.
Inputs
- sourceUrl: source URL as defined by Uniform
- sourceWildcards: array of defined wildcards, each having a
pathSegment
, the text value of wildcard andindex
where it occurs in the source URL - targetUrl: target URL as defined by Uniform
- targetWildcards: array of defined wildcards, each having a
pathSegment
, the text value of wildcard andindex
where it occurs in the target URL
Outputs
- sourceUrl: source URL with wildcards converted into a new form
- targetUrl: target URL with wildcards converted into a new form
const wildcardConverter = ({
sourceUrl,
sourceWildcards,
targetUrl,
targetWildcards,
}) => {
const wildcardPosition = new Map()
let count = 1
sourceWildcards.forEach((w) => {
if (w.pathSegment === '*') return
wildcardPosition.set(w.pathSegment, count++)
sourceUrl = sourceUrl.replace(w.pathSegment, '*')
})
if (
sourceWildcards.length &&
sourceWildcards[sourceWildcards.length - 1].pathSegment === '*'
) {
wildcardPosition.set(':splat', count)
}
targetWildcards.forEach((w) => {
targetUrl = targetUrl.replace(
w.pathSegment,
`$${wildcardPosition.get(w.pathSegment)}`
)
})
return { sourceUrl, targetUrl }
}
writeFile#
Take the converted Uniform objects to be processed.
Input
- redirects: array of converted objects.
function writeFile(redirects) {
if (!fs.existsSync('out')) {
fs.mkdirSync('out')
}
fs.writeFile(
'out/nextJsRedirects.json',
JSON.stringify(redirects, undefined, ' '),
(e) => {
if (e) {
// eslint-disable-next-line no-console
console.log(e)
}
}
)
}
Examples#
Convert Uniform to Next.js redirect format
RedirectFileConverter({
redirectEntryObject: (r) => ({
source: r.redirect.sourceUrl,
destination: r.redirect.targetUrl,
permanent: r.redirect.targetStatusCode === 301,
}),
writeFile(redirects) {
fs.writeFile(
'out/nextJsRedirects.json',
JSON.stringify(redirects, null, 4),
(e) => {
if (e) {
console.log(e)
}
}
)
},
})