Skip to content

Commit

Permalink
Rename consumerIdentifier to apitallyConsumer (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimon committed Jun 27, 2024
1 parent 6eb5087 commit 8b56b8f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 14 deletions.
11 changes: 9 additions & 2 deletions src/express/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import listEndpoints from "./listEndpoints.js";

declare module "express" {
interface Request {
consumerIdentifier?: string;
apitallyConsumer?: string;
consumerIdentifier?: string; // For backwards compatibility
}
}

Expand Down Expand Up @@ -121,7 +122,13 @@ const getMiddleware = (app: Express, client: ApitallyClient) => {
};

const getConsumer = (req: Request) => {
return req.consumerIdentifier ? String(req.consumerIdentifier) : null;
if (req.apitallyConsumer) {
return String(req.apitallyConsumer);
} else if (req.consumerIdentifier) {
// For backwards compatibility
return String(req.consumerIdentifier);
}
return null;
};

const extractExpressValidatorErrors = (responseBody: any) => {
Expand Down
14 changes: 11 additions & 3 deletions src/fastify/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ declare module "fastify" {
}

interface FastifyRequest {
consumerIdentifier?: string;
apitallyConsumer?: string;
consumerIdentifier?: string; // For backwards compatibility
}
}

Expand All @@ -23,7 +24,8 @@ const apitallyPlugin: FastifyPluginAsync<ApitallyConfig> = async (
const client = new ApitallyClient(config);
const routes: PathInfo[] = [];

fastify.decorateRequest("consumerIdentifier", null);
fastify.decorateRequest("apitallyConsumer", null);
fastify.decorateRequest("consumerIdentifier", null); // For backwards compatibility
fastify.decorateReply("payload", null);

fastify.addHook("onRoute", (routeOptions) => {
Expand Down Expand Up @@ -138,7 +140,13 @@ const getAppInfo = (routes: PathInfo[], appVersion?: string) => {
};

const getConsumer = (request: FastifyRequest) => {
return request.consumerIdentifier ? String(request.consumerIdentifier) : null;
if (request.apitallyConsumer) {
return String(request.apitallyConsumer);
} else if (request.consumerIdentifier) {
// For backwards compatibility
return String(request.consumerIdentifier);
}
return null;
};

const extractAjvErrors = (message: string): ValidationError[] => {
Expand Down
10 changes: 7 additions & 3 deletions src/koa/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ const getPath = (ctx: Koa.Context) => {
};

const getConsumer = (ctx: Koa.Context) => {
return ctx.state.consumerIdentifier
? String(ctx.state.consumerIdentifier)
: null;
if (ctx.state.apitallyConsumer) {
return String(ctx.state.apitallyConsumer);
} else if (ctx.state.consumerIdentifier) {
// For backwards compatibility
return String(ctx.state.consumerIdentifier);
}
return null;
};

const getAppInfo = (app: Koa, appVersion?: string): AppInfo => {
Expand Down
6 changes: 3 additions & 3 deletions tests/express/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { CLIENT_ID, ENV } from "../utils.js";

declare module "express" {
interface Request {
consumerIdentifier?: string;
apitallyConsumer?: string;
}
}

Expand All @@ -32,7 +32,7 @@ export const getAppWithCelebrate = () => {
{ abortEarly: false },
),
(req: Request, res) => {
req.consumerIdentifier = "test";
req.apitallyConsumer = "test";
res.send(
`Hello ${req.query?.name}! You are ${req.query?.age} years old!`,
);
Expand Down Expand Up @@ -79,7 +79,7 @@ export const getAppWithValidator = () => {
query("name").isString().isLength({ min: 2 }),
query("age").isInt({ min: 18 }),
(req: Request, res) => {
req.consumerIdentifier = "test";
req.apitallyConsumer = "test";
const result = validationResult(req);
if (result.isEmpty()) {
return res.send(
Expand Down
2 changes: 1 addition & 1 deletion tests/fastify/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const getApp = async () => {
},
async function (request) {
const { name, age } = request.query;
request.consumerIdentifier = "test";
request.apitallyConsumer = "test";
return `Hello ${name}! You are ${age} years old!`;
},
);
Expand Down
4 changes: 2 additions & 2 deletions tests/koa/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const getAppWithKoaRouter = () => {
});

router.get("/hello", async (ctx) => {
ctx.state.consumerIdentifier = "test";
ctx.state.apitallyConsumer = "test";
ctx.body = `Hello ${ctx.query.name}! You are ${ctx.query.age} years old!`;
});
router.get("/hello/:id", async (ctx) => {
Expand Down Expand Up @@ -50,7 +50,7 @@ export const getAppWithKoaRoute = () => {
app.use(bodyParser());
app.use(
route.get("/hello", async (ctx) => {
ctx.state.consumerIdentifier = "test";
ctx.state.apitallyConsumer = "test";
ctx.body = `Hello ${ctx.query.name}! You are ${ctx.query.age} years old!`;
}),
);
Expand Down

0 comments on commit 8b56b8f

Please sign in to comment.