Skip to content

Commit

Permalink
add deprecation warning and redirects for deprecated routes
Browse files Browse the repository at this point in the history
  • Loading branch information
sdumetz committed Jul 8, 2024
1 parent 2b2da95 commit a404817
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions source/server/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

import path from "path";
import util from "util";

import cookieSession from "cookie-session";
import express from "express";
import bodyParser from "body-parser";


import UserManager from "../auth/UserManager.js";
import { BadRequestError, HTTPError } from "../utils/errors.js";
Expand Down Expand Up @@ -207,7 +208,27 @@ export default async function createServer(config = defaultConfig) :Promise<expr
app.use("/scenes", (await import("./scenes/index.js")).default);
app.use("/users", (await import("./users/index.js")).default);


/**
* Redirects for previous routes under /api/v1 prefix
* @fixme remove on next major release
*/
app.use("/api/v1", util.deprecate((req, res, next)=>{
// Signal deprecation using the non-standard Deprecation header
// https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-deprecation-header
res.set("Deprecation", "@"+Math.round(new Date("2024-07-08T00:00:00.000Z").getTime()/1000).toString());
//Expected sunset date, approximately
res.set("Sunset", 'Wed, 01 Jan 2025 00:00:00 GMT');
let pathname = req.originalUrl.replace("/api/v1/", "/");
if(/^\/log(?:in|out)/.test(pathname)){
pathname = "/auth"+pathname;
}else if(pathname.endsWith("history")){
pathname = "/history"+pathname.replace("/scenes", "").replace("/history", "");
}else if(pathname.endsWith("/permissions")){
pathname = "/auth/access"+pathname.replace("/scenes", "").replace("/permissions", "");
}
console.log('Path : %s => %s', req.originalUrl, pathname);
res.redirect(301, pathname);
}, `/api/v1 routes are deprecated. Use the new shorter naming scheme`));

const log_errors = process.env["TEST"] !== 'true';
const isTTY = process.stderr.isTTY;
Expand Down

0 comments on commit a404817

Please sign in to comment.