Skip to content

Commit

Permalink
feat: add zod validation for api param
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorusclarence committed Nov 25, 2022
1 parent ceed899 commit 2f26272
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"react-table": "^7.8.0",
"react-tippy": "^1.4.0",
"react-twitter-widgets": "^1.11.0",
"swr": "^1.3.0"
"swr": "^1.3.0",
"zod": "^3.19.1"
},
"devDependencies": {
"@commitlint/cli": "^13.2.1",
Expand Down
3 changes: 2 additions & 1 deletion src/pages/api/content/[slug].ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { z } from 'zod';

import { getUserLikeCount } from '@/lib/api.server';
import { getArticleViewsFromDevto } from '@/lib/devto';
Expand All @@ -9,7 +10,7 @@ export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const slug = req.query.slug as string;
const slug = z.string().parse(req.query.slug);
const sessionId = getSessionId(req);

try {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/api/like/[slug].ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { z } from 'zod';

import { getUserLikeCount } from '@/lib/api.server';
import { getArticleViewsFromDevto } from '@/lib/devto';
Expand All @@ -9,7 +10,7 @@ export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const slug = req.query.slug as string;
const slug = z.string().parse(req.query.slug);
const sessionId = getSessionId(req);

try {
Expand Down
5 changes: 3 additions & 2 deletions src/pages/api/newsletter/add.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// eslint-disable-next-line unused-imports/no-unused-imports
import axios, { AxiosError } from 'axios';
import { NextApiRequest, NextApiResponse } from 'next';
import { z } from 'zod';

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method === 'POST') {
const data = req.body;
const email = z.string().email().parse(req.body.email);

try {
const response = await axios.post(
'https://www.getrevue.co/api/v2/subscribers',
{ ...data, double_opt_in: true },
{ email, double_opt_in: true },
{
headers: {
'Content-Type': 'application/json',
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6662,6 +6662,11 @@ yocto-queue@^0.1.0:
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==

zod@^3.19.1:
version "3.19.1"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.19.1.tgz#112f074a97b50bfc4772d4ad1576814bd8ac4473"
integrity sha512-LYjZsEDhCdYET9ikFu6dVPGp2YH9DegXjdJToSzD9rO6fy4qiRYFoyEYwps88OseJlPyl2NOe2iJuhEhL7IpEA==

zwitch@^2.0.0:
version "2.0.2"
resolved "https://registry.yarnpkg.com/zwitch/-/zwitch-2.0.2.tgz#91f8d0e901ffa3d66599756dde7f57b17c95dce1"
Expand Down

0 comments on commit 2f26272

Please sign in to comment.