Skip to content

Commit

Permalink
Refactor deploy workflow to include API documentation and update code…
Browse files Browse the repository at this point in the history
… formatting settings
  • Loading branch information
jqshuv committed Oct 15, 2024
1 parent 299d866 commit 9ed7251
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 9 deletions.
178 changes: 178 additions & 0 deletions docs/api-docs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
openapi: 3.1.0
info:
title: short
version: 0.0.0
description: Short simple url shortener.

servers:
- url: https://s.jqshuv.com

paths:
/{shortcode}:
get:
operationId: redirectsToTheTargetUrl
tags:
- short
summary: Redirects to the target url
parameters:
- $ref: "#/components/parameters/shortcode"
responses:
"302":
description: The requested resource resides temporarily at a different URI, as
indicated by the Location header in the response.
content:
application/json:
schema:
type: object
properties:
status:
type: string
message:
type: string
location:
type: string
required:
- status
- message
- location
"":
post:
operationId: createANewRedirect
tags:
- short
summary: Create a new redirect
responses:
"201":
description: Created - Resource successfully created
content:
application/json:
schema:
type: object
properties:
shortcode:
type: string
redirect:
type: string
status:
type: string
required:
- data
- shortcode
- redirect
requestBody:
required: true
description: ""
content:
application/json:
schema:
type: object
properties:
redirect:
type: string
shortcode:
type: string
required:
- redirect
security:
- Authorization with Header: []
- Authorization with Query: []
parameters: []
put:
operationId: updatesAShortcode
tags:
- short
summary: Updates a shortcode
responses:
"200":
description: OK - Resource successfully updated
content:
application/json:
schema:
type: object
properties:
status:
type: string
shortcode:
type: string
redirect:
type: string
required:
- data
- shortcode
- redirect
"404":
$ref: "#/components/responses/NotFound"
requestBody:
required: true
description: Updates a redirect url.
content:
application/json:
schema:
type: object
properties:
shortcode:
type: string
redirect:
type: string
required:
- shortcode
- redirect
security:
- Authorization with Header: []
- Authorization with Query: []
parameters: []
delete:
operationId: deletesAShortcode
tags:
- short
summary: Deletes a shortcode
responses:
"204":
$ref: "#/components/responses/NoContent"
security:
- Authorization with Header: []
- Authorization with Query: []

components:
parameters:
shortcode:
name: shortcode
in: path
required: true
schema:
type: string
securitySchemes:
Authorization with Header:
type: apiKey
name: Authorization with Header
in: header
Authorization with Query:
type: apiKey
name: Authorization with Query
in: query
responses:
NotFound:
description: The server cannot find the requested resource. The endpoint may be
invalid or the resource may no longer exist.
content:
application/json:
schema:
type: object
properties:
message:
type: string
required:
- message
NoContent:
description: The request was successful, but there is no content to return in
the response.
content:
application/json:
schema:
type: object
properties:
status:
type: string
required: []
tags:
- name: short
22 changes: 14 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default {
async fetch(request, env, ctx): Promise<Response> {
const requestUrl = new URL(request.url);
const shortCodeGet = requestUrl.pathname.slice(1);
let body: { shortCode?: string, redirect: string } | undefined;
let body: { shortcode?: string, redirect: string } | undefined;

// check if request has body and await for it to be parsed to variable body

Expand All @@ -61,7 +61,7 @@ export default {
return new Response('Bad Request', { status: 400 });
}

body = await request.json() as { shortCode?: string, redirect: string } || undefined;
body = await request.json() as { shortcode?: string, redirect: string } || undefined;

if (!body || !body.redirect) {
return new Response('Bad Request', { status: 400 });
Expand All @@ -75,12 +75,12 @@ export default {
switch (request.method) {
case 'POST':
if (!body) return new Response('Bad Request', { status: 400 });
const postShortCode = body.shortCode || Math.random().toString(36).slice(2, 8);
const postShortCode = body.shortcode || Math.random().toString(36).slice(2, 8);
const postRedirectUrl = body.redirect;

await env.SHORT_URLS.put(postShortCode, postRedirectUrl);
// response with shortCode in json
return new Response(JSON.stringify({ shortCode: postShortCode }), { status: 200 });
return new Response(JSON.stringify({ status: "succesfully_created",shortcode: postShortCode, redirect: postRedirectUrl }), { status: 201 });
case 'GET':
if (!shortCodeGet) return new Response('just redirect. - powered by jqshuv x unately.', { status: 200 });

Expand All @@ -92,19 +92,25 @@ export default {
}
case 'PUT':
if (!body) return new Response('Bad Request', { status: 400 });
const putShortCode = body.shortCode || Math.random().toString(36).slice(2, 8);
const putShortCode = body.shortcode;
const putRedirectUrl = body.redirect;

if (!putShortCode) return new Response('Bad Request', { status: 400 });

const existingData = await env.SHORT_URLS.get(putShortCode);

if (!existingData) return new Response('Not Found', { status: 404 })

await env.SHORT_URLS.put(putShortCode, putRedirectUrl);
return new Response(JSON.stringify({ shortCode: putShortCode }), { status: 200 });
return new Response(JSON.stringify({ status: "succesfully_updated", shortcode: putShortCode, redirect: putRedirectUrl }), { status: 200 });
case 'DELETE':
if (!body) return new Response('Bad Request', { status: 400 });
const deleteShortCode = body.shortCode
const deleteShortCode = body.shortcode

if (!deleteShortCode) return new Response('Bad Request', { status: 400 });

await env.SHORT_URLS.delete(deleteShortCode);
return new Response('Deleted', { status: 200 });
return new Response(JSON.stringify({ status: "succesfully_deleted" }), { status: 200 });
default:
return new Response('Method Not Allowed', { status: 405 });
}
Expand Down
2 changes: 1 addition & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Hello World worker', () => {
const ctx = createExecutionContext();
const response = await worker.fetch(request, env, ctx);
await waitOnExecutionContext(ctx);
expect(response.status).toBe(200);
expect(response.status).toBe(201);
});


Expand Down

0 comments on commit 9ed7251

Please sign in to comment.