Skip to content

Commit

Permalink
Convert to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma committed May 20, 2022
1 parent bb7079d commit a21b8fd
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 14 deletions.
6 changes: 3 additions & 3 deletions functions/_middleware.js → functions/_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

import { HTTPError } from "..";
import { Environment, HTTPError } from "..";

export async function onRequest({ next }) {
export const onRequest: PagesFunction<Environment> = async ({ next }) => {
try {
return await next();
} catch (e) {
Expand All @@ -41,4 +41,4 @@ export async function onRequest({ next }) {
const data = JSON.stringify(json);
return new Response(data, { headers, status: e.status });
}
}
};
6 changes: 3 additions & 3 deletions functions/documents/[id].js → functions/documents/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

import { HTTPError } from "../..";
import { Environment, HTTPError } from "../..";

export async function onRequestGet({ params, env }) {
export const onRequestGet: PagesFunction<Environment> = async ({ params, env }) => {
const content = await env.STORAGE.get(`documents:${params.id}`);

if (content) {
Expand All @@ -38,4 +38,4 @@ export async function onRequestGet({ params, env }) {
}

throw new HTTPError(404, `Document "${params.id}" not found.`);
}
};
8 changes: 4 additions & 4 deletions functions/documents/index.js → functions/documents/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

import { HTTPError } from "../..";
import { Environment, HTTPError } from "../..";

function generateId(size) {
function generateId(size: number): string {
let id = "";
const keyspace = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

Expand All @@ -35,7 +35,7 @@ function generateId(size) {
return id;
}

export async function onRequestPost({ request, env }) {
export const onRequestPost: PagesFunction<Environment> = async ({ request, env }) => {
const length = Number(request.headers.get("Content-Length") || 0);

if (!length) {
Expand Down Expand Up @@ -63,4 +63,4 @@ export async function onRequestPost({ request, env }) {

const data = JSON.stringify(json);
return new Response(data, { headers, status: 200 });
}
};
6 changes: 3 additions & 3 deletions functions/raw/[id].js → functions/raw/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

import { HTTPError } from "../..";
import { Environment, HTTPError } from "../..";

export async function onRequestGet({ params, env }) {
export const onRequestGet: PagesFunction<Environment> = async ({ params, env }) => {
const content = await env.STORAGE.get(`documents:${params.id}`);

if (content) {
Expand All @@ -35,4 +35,4 @@ export async function onRequestGet({ params, env }) {
}

throw new HTTPError(404, `Document "${params.id}" not found.`);
}
};
12 changes: 11 additions & 1 deletion index.js → index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,18 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

export type Environment = {
STORAGE: KVNamespace;

DOCUMENT_KEY_SIZE: number;
MAX_DOCUMENT_SIZE: number;
DOCUMENT_EXPIRE_TTL: number;
};

export class HTTPError extends Error {
constructor(status, message) {
status: number;

constructor(status: number, message: string) {
super(message);
this.status = status;
}
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"homepage": "https://starb.in",
"devDependencies": {
"@cloudflare/workers-types": "^3.10.0",
"prettier": "^2.2.1",
"wrangler": "^2.0.5"
}
Expand Down
12 changes: 12 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "CommonJS",
"lib": [
"ES2020"
],
"types": [
"@cloudflare/workers-types"
]
}
}

0 comments on commit a21b8fd

Please sign in to comment.