Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename consumerIdentifier to apitallyConsumer #17

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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

Expand Down Expand Up @@ -121,7 +122,13 @@
};

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);
}

Check warning on line 130 in src/express/middleware.ts

View check run for this annotation

Codecov / codecov/patch

src/express/middleware.ts#L128-L130

Added lines #L128 - L130 were not covered by tests
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 @@
}

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

Expand All @@ -23,7 +24,8 @@
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 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);
}

Check warning on line 148 in src/fastify/plugin.ts

View check run for this annotation

Codecov / codecov/patch

src/fastify/plugin.ts#L146-L148

Added lines #L146 - L148 were not covered by tests
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 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);
}

Check warning on line 73 in src/koa/middleware.ts

View check run for this annotation

Codecov / codecov/patch

src/koa/middleware.ts#L71-L73

Added lines #L71 - L73 were not covered by tests
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