Skip to content

Commit

Permalink
fix: replace status text responses with text responses (messages are …
Browse files Browse the repository at this point in the history
…sent as the body)
  • Loading branch information
jacob-shuman committed Oct 2, 2024
1 parent b008ddd commit 34b8d74
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 159 deletions.
94 changes: 48 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ I'm still looking into how rss aggregators generally handle auth for feeds and o
}
```

> For the most up to date definition, see [the actual typescript type](./src/lib/types.ts#L16).
## :heavy_plus_sign: Add an article

:lock: **Requires Authentication**
Expand Down Expand Up @@ -125,12 +127,12 @@ You can add an article by providing the article's url in the body of a `POST` re

### Responses

| Status | StatusText | Body | Content-Type |
| ------ | ------------------------------------- | ----------- | ------------------ |
| 200 | `article added successfully` | `undefined` | `application/json` |
| 400 | `url is required` | `undefined` | `undefined` |
| 400 | `unable to extract metadata at {url}` | `undefined` | `undefined` |
| 401 | `not authorized` | `undefined` | `undefined` |
| Status | Body | Content-Type |
| ------ | ------------------------------------- | ------------ |
| 200 | `article added successfully` | `text/plain` |
| 400 | `url is required` | `text/plain` |
| 400 | `unable to extract metadata at {url}` | `text/plain` |
| 401 | `not authorized` | `text/plain` |

## :page_facing_up: Get a single `JSON` article

Expand All @@ -142,11 +144,11 @@ You can get a single `JSON` object representing an article by making a `GET` req

### Responses

| Status | StatusText | Body | Content-Type |
| ------ | ----------------------------------------- | ----------- | ------------------ |
| 200 | `undefined` | `Article` | `application/json` |
| 401 | `not authorized` | `undefined` | `undefined` |
| 404 | `there is no article with an id of ":id"` | `undefined` | `undefined` |
| Status | Body | Content-Type |
| ------ | ----------------------------------------- | ------------------ |
| 200 | [Article](./src/lib/types.ts#L16) | `application/json` |
| 401 | `not authorized` | `text/plain` |
| 404 | `there is no article with an id of ":id"` | `text/plain` |

## :clipboard: Get a `JSON` array of all articles

Expand All @@ -158,10 +160,10 @@ You can get a `JSON` array of articles by making a `GET` request to the `/articl

### Responses

| Status | StatusText | Body | Content-Type |
| ------ | ---------------- | ----------- | ------------------ |
| 200 | `undefined` | `Article[]` | `application/json` |
| 401 | `not authorized` | `undefined` | `undefined` |
| Status | Body | Content-Type |
| ------ | ----------------------------------- | ------------------ |
| 200 | [Article[]](./src/lib/types.ts#L16) | `application/json` |
| 401 | `not authorized` | `text/plain` |

## :memo: Update an article

Expand Down Expand Up @@ -191,11 +193,11 @@ You can update an article based on it's id by making a `PATCH` request to the `/

### Responses

| Status | StatusText | Body | Content-Type |
| ------ | ------------------------------------ | ----------- | ------------ |
| 200 | `article :id deleted successfully` | `undefined` | `undefined` |
| 401 | `not authorized` | `undefined` | `undefined` |
| 404 | `there is no article with id of :id` | `undefined` | `undefined` |
| Status | Body | Content-Type |
| ------ | ------------------------------------ | ------------ |
| 200 | `article :id deleted successfully` | `text/plain` |
| 401 | `not authorized` | `text/plain` |
| 404 | `there is no article with id of :id` | `text/plain` |

## :wastebasket: Delete an article

Expand All @@ -207,11 +209,11 @@ You can delete an article based on it's id by making a `DELETE` request to the `

### Responses

| Status | StatusText | Body | Content-Type |
| ------ | ------------------------------------ | ----------- | ------------ |
| 200 | `article :id deleted successfully` | `undefined` | `undefined` |
| 401 | `not authorized` | `undefined` | `undefined` |
| 404 | `there is no article with id of :id` | `undefined` | `undefined` |
| Status | Body | Content-Type |
| ------ | ------------------------------------ | ------------ |
| 200 | `article :id deleted successfully` | `text/plain` |
| 401 | `not authorized` | `text/plain` |
| 404 | `there is no article with id of :id` | `text/plain` |

## :wastebasket: Remove all articles

Expand All @@ -221,10 +223,10 @@ You can delete an article based on it's id by making a `DELETE` request to the `

### Responses

| Status | StatusText | Body | Content-Type |
| ------ | --------------------------------- | ----------- | ------------ |
| 200 | `x articles cleared successfully` | `undefined` | `undefined` |
| 401 | `not authorized` | `undefined` | `undefined` |
| Status | Body | Content-Type |
| ------ | --------------------------------- | ------------ |
| 200 | `x articles cleared successfully` | `text/plain` |
| 401 | `not authorized` | `text/plain` |

## :wastebasket: Purge old articles

Expand Down Expand Up @@ -253,11 +255,11 @@ Examples

### Responses

| Status | StatusText | Body | Content-Type |
| ------ | -------------------------------------------------------------- | ----------- | ------------ |
| 200 | `x articles purged successfully` | `undefined` | `undefined` |
| 400 | `invalid format, use the formula "<number><h \| d \| m \| y>"` | `undefined` | `undefined` |
| 401 | `not authorized` | `undefined` | `undefined` |
| Status | Body | Content-Type |
| ------ | -------------------------------------------------------------- | ------------ |
| 200 | `x articles purged successfully` | `text/plain` |
| 400 | `invalid format, use the formula "<number><h \| d \| m \| y>"` | `text/plain` |
| 401 | `not authorized` | `text/plain` |

## :inbox_tray: Generate RSS2 feed from articles

Expand All @@ -271,29 +273,29 @@ Examples

### Responses

| Status | StatusText | Body | Content-Type |
| ------ | ----------- | -------- | --------------------- |
| 200 | `undefined` | RSS Feed | `application/rss+xml` |
| Status | Body | Content-Type |
| ------ | ------------------------------------------------------- | --------------------- |
| 200 | [RSS2 Feed](https://www.rssboard.org/rss-specification) | `application/rss+xml` |

## :inbox_tray: Generate Atom feed from articles

`GET (http|https)://HOST:PORT/atom`

### Responses

| Status | StatusText | Body | Content-Type |
| ------ | ----------- | --------- | ---------------------- |
| 200 | `undefined` | Atom Feed | `application/atom+xml` |
| Status | Body | Content-Type |
| ------ | --------------------------------------------------------- | ---------------------- |
| 200 | [Atom Feed](https://validator.w3.org/feed/docs/atom.html) | `application/atom+xml` |

## :inbox_tray: Generate JSON feed from articles

`GET (http|https)://HOST:PORT/json`

### Responses

| Status | StatusText | Body | Content-Type |
| ------ | ----------- | --------- | ------------------ |
| 200 | `undefined` | JSON Feed | `application/json` |
| Status | Body | Content-Type |
| ------ | -------------------------------------- | ------------------ |
| 200 | [JSON Feed](https://www.jsonfeed.org/) | `application/json` |

## :heart: Health

Expand All @@ -303,6 +305,6 @@ A simple `GET` route to see if the server is up and ready to handle incoming req

### Responses

| Status | StatusText | Body | Content-Type |
| ------ | ---------- | ----------- | ------------ |
| 200 | `OK` | `undefined` | `undefined` |
| Status | Body | Content-Type |
| ------ | ---- | ------------ |
| 200 | `OK` | `text/plain` |
11 changes: 5 additions & 6 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { env } from '$env/dynamic/private';
import { logger } from '$lib/utils';
import type { Handle } from '@sveltejs/kit';
import { text, type Handle } from '@sveltejs/kit';

export const handle: Handle = async ({ event, resolve }) => {
if (env.PASSWORD && !env.AUTH_SECRET) {
logger.error(
'You must set $AUTH_SECRET since you set $PASSWORD ($AUTH_SECRET is used to sign JWTs!)'
);

return new Response(undefined, {
status: 500,
statusText:
'You must set $AUTH_SECRET since you set $PASSWORD ($AUTH_SECRET is used to sign JWTs!)'
});
return text(
'You must set $AUTH_SECRET since you set $PASSWORD ($AUTH_SECRET is used to sign JWTs!)',
{ status: 500, headers: { 'Content-Type': 'text/plain' } }
);
}

return await resolve(event);
Expand Down
11 changes: 3 additions & 8 deletions src/routes/articles/+server.ts
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 });
};
15 changes: 6 additions & 9 deletions src/routes/articles/[articleId]/+server.ts
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 });
};
12 changes: 6 additions & 6 deletions src/routes/articles/[articleId]/delete/+server.ts
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' }
});
};
17 changes: 6 additions & 11 deletions src/routes/articles/[articleId]/update/+server.ts
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 });
};
22 changes: 13 additions & 9 deletions src/routes/articles/add/+server.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { isAuthorized } from '$lib/auth';
import { addArticle } from '$lib/db';
import { extract } from '@extractus/article-extractor';
import { type RequestHandler } from '@sveltejs/kit';
import { text, type RequestHandler } from '@sveltejs/kit';

export const POST: RequestHandler = async ({ request, cookies }) => {
if (!request.body) {
return text('url is required', { status: 400, headers: { 'Content-Type': 'text/plain' } });
}

const { url } = await request.json();

if (!isAuthorized({ request, cookies })) {
return new Response(undefined, { status: 401, statusText: 'not authorized' });
} else if (!url) {
return new Response(undefined, { status: 400, statusText: 'url is required' });
if (!(await isAuthorized({ request, cookies }))) {
return text('not authorized', { status: 401, headers: { 'Content-Type': 'text/plain' } });
} else if (!request.body || !url) {
return text('url is required', { status: 400, headers: { 'Content-Type': 'text/plain' } });
}

const article = await extract(url);
Expand All @@ -27,14 +31,14 @@ export const POST: RequestHandler = async ({ request, cookies }) => {
ttr: article.ttr
});

return new Response(undefined, {
return text('article added successfully', {
status: 200,
statusText: 'article added successfully'
headers: { 'Content-Type': 'text/plain' }
});
}

return new Response(undefined, {
return text(`unable to extract metadata at "${url}"`, {
status: 400,
statusText: `unable to extract metadata at "${url}"`
headers: { 'Content-Type': 'text/plain' }
});
};
8 changes: 4 additions & 4 deletions src/routes/articles/clear/+server.ts
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' }
});
};
Loading

0 comments on commit 34b8d74

Please sign in to comment.