-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: replace status text responses with text responses (messages are …
…sent as the body)
- Loading branch information
1 parent
b008ddd
commit 34b8d74
Showing
16 changed files
with
129 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,11 @@ | ||
import { isAuthorized } from '$lib/auth'; | ||
import { getArticles } from '$lib/db'; | ||
import { type RequestHandler } from '@sveltejs/kit'; | ||
import { json, text, type RequestHandler } from '@sveltejs/kit'; | ||
|
||
export const GET: RequestHandler = async ({ request, cookies }) => { | ||
if (!(await isAuthorized({ request, cookies }))) { | ||
return new Response(undefined, { status: 401, statusText: 'not authorized' }); | ||
return text('not authorized', { status: 401, headers: { 'Content-Type': 'text/plain' } }); | ||
} | ||
|
||
return new Response(JSON.stringify(await getArticles()), { | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
status: 200 | ||
}); | ||
return json(await getArticles(), { status: 200 }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,22 @@ | ||
import { isAuthorized } from '$lib/auth'; | ||
import { getArticle } from '$lib/db'; | ||
import { type RequestHandler } from '@sveltejs/kit'; | ||
import { json, text, type RequestHandler } from '@sveltejs/kit'; | ||
|
||
export const GET: RequestHandler = async ({ params, request, cookies }) => { | ||
const { articleId } = params; | ||
|
||
if (!(await isAuthorized({ request, cookies }))) { | ||
return new Response(undefined, { status: 401, statusText: 'not authorized' }); | ||
return text('not authorized', { status: 401, headers: { 'Content-Type': 'text/plain' } }); | ||
} | ||
|
||
const article = getArticle(Number(articleId)); | ||
const article = await getArticle(Number(articleId)); | ||
|
||
if (!article) { | ||
return new Response(undefined, { | ||
return text(`there is no article with an id of "${articleId}"`, { | ||
status: 404, | ||
statusText: `there is no article with an id of "${articleId}"` | ||
headers: { 'Content-Type': 'text/plain' } | ||
}); | ||
} | ||
|
||
return new Response(JSON.stringify(article), { | ||
headers: { 'Content-Type': 'application/json' }, | ||
status: 200 | ||
}); | ||
return json(article, { status: 200 }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,25 @@ | ||
import { isAuthorized } from '$lib/auth'; | ||
import { deleteArticle } from '$lib/db'; | ||
import { type RequestHandler } from '@sveltejs/kit'; | ||
import { text, type RequestHandler } from '@sveltejs/kit'; | ||
|
||
export const DELETE: RequestHandler = async ({ request, params, cookies }) => { | ||
const { articleId } = params; | ||
|
||
if (!(await isAuthorized({ request, cookies }))) { | ||
return new Response(undefined, { status: 401, statusText: 'not authorized' }); | ||
return text('not authorized', { status: 401, headers: { 'Content-Type': 'text/plain' } }); | ||
} | ||
|
||
const [{ numDeletedRows }] = await deleteArticle(Number(articleId)); | ||
|
||
if (numDeletedRows < 1) { | ||
return new Response(undefined, { | ||
return text(`there is no article with id of ${articleId}`, { | ||
status: 404, | ||
statusText: `there is no article with id of ${articleId}` | ||
headers: { 'Content-Type': 'text/plain' } | ||
}); | ||
} | ||
|
||
return new Response(undefined, { | ||
return text(`article ${articleId} deleted successfully`, { | ||
status: 200, | ||
statusText: `article ${articleId} deleted successfully` | ||
headers: { 'Content-Type': 'text/plain' } | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,23 @@ | ||
import { isAuthorized } from '$lib/auth'; | ||
import { updateArticle } from '$lib/db'; | ||
import { type RequestHandler } from '@sveltejs/kit'; | ||
import { json, text, type RequestHandler } from '@sveltejs/kit'; | ||
|
||
export const PATCH: RequestHandler = async ({ request, params, cookies }) => { | ||
const { articleId } = params; | ||
const { article } = await request.json(); | ||
|
||
if (!(await isAuthorized({ request, cookies }))) { | ||
return new Response(undefined, { status: 401, statusText: 'not authorized' }); | ||
return text('not authorized', { status: 401, headers: { 'Content-Type': 'text/plain' } }); | ||
} | ||
|
||
const updatedArticle = updateArticle(Number(articleId), article); | ||
const updatedArticle = await updateArticle(Number(articleId), article); | ||
|
||
if (!updatedArticle) { | ||
return new Response(undefined, { | ||
return text(`there is no article with id of ${articleId}`, { | ||
status: 404, | ||
statusText: `there is no article with id of ${articleId}` | ||
headers: { 'Content-Type': 'text/plain' } | ||
}); | ||
} | ||
|
||
return new Response(JSON.stringify(updatedArticle), { | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
status: 200 | ||
}); | ||
return json(updatedArticle, { status: 200 }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import { isAuthorized } from '$lib/auth'; | ||
import { deleteAllArticles } from '$lib/db'; | ||
import { type RequestHandler } from '@sveltejs/kit'; | ||
import { text, type RequestHandler } from '@sveltejs/kit'; | ||
|
||
export const DELETE: RequestHandler = async ({ request, cookies }) => { | ||
if (!(await isAuthorized({ request, cookies }))) { | ||
return new Response(undefined, { status: 401, statusText: 'not authorized' }); | ||
return text('not authorized', { status: 401, headers: { 'Content-Type': 'text/plain' } }); | ||
} | ||
|
||
const [{ numDeletedRows }] = await deleteAllArticles(); | ||
|
||
return new Response(undefined, { | ||
return text(`${numDeletedRows} articles cleared successfully`, { | ||
status: 200, | ||
statusText: `${numDeletedRows} articles cleared successfully` | ||
headers: { 'Content-Type': 'text/plain' } | ||
}); | ||
}; |
Oops, something went wrong.