-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from devondragon/issue-22-Session-state-threw-…
…an-exception Issue 22 session state threw an exception
- Loading branch information
Showing
5 changed files
with
179 additions
and
92 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Env } from './env'; | ||
import { IRequest } from 'itty-router'; | ||
import { createSession, getSessionData, deleteSession, updateSession, addToSession } from './session'; | ||
|
||
// Constants for HTTP status codes | ||
const STATUS_CREATED = 201; | ||
const STATUS_OK = 200; | ||
const STATUS_NOT_FOUND = 404; | ||
const STATUS_INTERNAL_SERVER_ERROR = 500; | ||
|
||
// Utility function to parse JSON safely | ||
async function parseJson(request: IRequest): Promise<any> { | ||
try { | ||
return await request.json(); | ||
} catch (error) { | ||
console.error('Failed to parse JSON:', error); | ||
throw new Error('Invalid JSON'); | ||
} | ||
} | ||
|
||
// Handle Create Session | ||
export async function handleCreateSession(request: IRequest, env: Env): Promise<Response> { | ||
try { | ||
const requestData = await parseJson(request); | ||
const sessionId = await createSession(requestData, env); | ||
return new Response(sessionId, { status: STATUS_CREATED }); | ||
} catch (error) { | ||
console.error('Error in handleCreateSession:', error); | ||
return new Response('Failed to create session', { status: STATUS_INTERNAL_SERVER_ERROR }); | ||
} | ||
} | ||
|
||
// Handle Get Session | ||
export async function handleGetSessionData(request: IRequest, env: Env): Promise<Response> { | ||
try { | ||
const { sessionId } = request.params; | ||
const data = await getSessionData(sessionId, env); | ||
if (!data) { | ||
return new Response('Session not found', { status: STATUS_NOT_FOUND }); | ||
} | ||
return new Response(JSON.stringify(data), { status: STATUS_OK }); | ||
} catch (error) { | ||
console.error('Error in handleGetSessionData:', error); | ||
return new Response('Failed to retrieve session', { status: STATUS_INTERNAL_SERVER_ERROR }); | ||
} | ||
} | ||
|
||
// Handle Delete Session | ||
export async function handleDeleteSession(request: IRequest, env: Env): Promise<Response> { | ||
try { | ||
const { sessionId } = request.params; | ||
await deleteSession(sessionId, env); | ||
return new Response('Session deleted', { status: STATUS_OK }); | ||
} catch (error) { | ||
console.error('Error in handleDeleteSession:', error); | ||
return new Response('Failed to delete session', { status: STATUS_INTERNAL_SERVER_ERROR }); | ||
} | ||
} | ||
|
||
// Handle Update Session | ||
export async function handleUpdateSession(request: IRequest, env: Env): Promise<Response> { | ||
try { | ||
const { sessionId } = request.params; | ||
const requestData = await parseJson(request); | ||
await updateSession(sessionId, requestData, env); | ||
return new Response('Session updated', { status: STATUS_OK }); | ||
} catch (error) { | ||
console.error('Error in handleUpdateSession:', error); | ||
return new Response('Failed to update session', { status: STATUS_INTERNAL_SERVER_ERROR }); | ||
} | ||
} | ||
|
||
// Handle Add to Session | ||
export async function handleAddToSession(request: IRequest, env: Env): Promise<Response> { | ||
try { | ||
const { sessionId } = request.params; | ||
const requestData = await parseJson(request); | ||
await addToSession(sessionId, requestData, env); | ||
return new Response('Session updated with additional data', { status: STATUS_OK }); | ||
} catch (error) { | ||
console.error('Error in handleAddToSession:', error); | ||
return new Response('Failed to update session with additional data', { status: STATUS_INTERNAL_SERVER_ERROR }); | ||
} | ||
} |
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,66 +1,49 @@ | ||
/** | ||
* Provides session management functionality within a Cloudflare Workers environment. | ||
* This module defines methods for creating, retrieving, and deleting sessions, | ||
* This module defines methods for creating, retrieving, updating, adding to, and deleting sessions, | ||
* with each session identified by a unique session ID. Sessions are stored in a Cloudflare KV namespace. | ||
* | ||
* The `Env` interface represents the expected environment configuration, | ||
* containing the `sessionstore` KVNamespace for session data storage. | ||
* | ||
* Functions: | ||
* Functions (in the session.ts file) include: | ||
* - `generateSessionId`: Generates a unique session identifier using `crypto.randomUUID`. | ||
* - `createSession`: Creates a new session with the provided data in the Cloudflare KV store and returns the session ID. | ||
* - `updateSession`: Updates the data for a given session ID in the Cloudflare KV store. | ||
* - `addToSession`: Adds data to an existing session in the Cloudflare KV store. | ||
* - `getSessionData`: Retrieves session data for a given session ID from the Cloudflare KV store. | ||
* - `deleteSession`: Deletes a session from the Cloudflare KV store using the session ID. | ||
* | ||
* The default export is an async `fetch` function that handles HTTP requests to create, get, and delete sessions based on the request path. | ||
* It is designed to be deployed as part of a Cloudflare Worker. | ||
* The default export is an async `fetch` function that handles HTTP requests to create, get, update, add to, and delete sessions based on the request path. | ||
* It uses `itty-router` to define the routes and handle the requests. | ||
* The supported routes are: | ||
* - POST `/create`: Creates a new session. | ||
* - GET `/get/:sessionId`: Retrieves session data for the given session ID. | ||
* - DELETE `/delete/:sessionId`: Deletes the session with the given session ID. | ||
* - PUT `/update/:sessionId`: Updates the session data for the given session ID. | ||
* - PATCH `/add/:sessionId`: Adds data to the existing session with the given session ID. | ||
* - ALL `*`: Catches all other requests and returns a 404 response. | ||
* | ||
* It is designed to be deployed as part of a Cloudflare Worker. | ||
*/ | ||
import { AutoRouter, IRequest } from 'itty-router'; | ||
import { Env } from './env'; | ||
import { | ||
createSession, | ||
getSessionData, | ||
deleteSession, | ||
} from './session'; | ||
handleCreateSession, | ||
handleGetSessionData, | ||
handleAddToSession, | ||
handleUpdateSession, | ||
handleDeleteSession | ||
} from './handlers'; | ||
|
||
const router = AutoRouter<IRequest, [Env, ExecutionContext]>(); | ||
|
||
router | ||
.post('/create', async (request, env, ctx) => { | ||
try { | ||
const requestData = await request.json(); | ||
const sessionId = await createSession(requestData, env); | ||
return new Response(sessionId, { status: 201 }); | ||
} catch (error) { | ||
return new Response('Failed to create session', { status: 500 }); | ||
} | ||
}) | ||
.get('/get/:sessionId', async ({ params }, env) => { | ||
try { | ||
const { sessionId } = params; | ||
const data = await getSessionData(sessionId, env); | ||
if (!data) { | ||
return new Response('Session not found', { status: 404 }); | ||
} | ||
return new Response(JSON.stringify(data), { status: 200 }); | ||
} catch (error) { | ||
return new Response('Failed to retrieve session', { status: 500 }); | ||
} | ||
}) | ||
.delete('/delete/:sessionId', async ({ params }, env) => { | ||
try { | ||
const { sessionId } = params; | ||
await deleteSession(sessionId, env); | ||
return new Response('Session deleted', { status: 200 }); | ||
} catch (error) { | ||
return new Response('Failed to delete session', { status: 500 }); | ||
} | ||
}) | ||
.post('/create', (request, env) => handleCreateSession(request, env)) | ||
.get('/get/:sessionId', (request, env) => handleGetSessionData(request, env)) | ||
.delete('/delete/:sessionId', (request, env) => handleDeleteSession(request, env)) | ||
.put('/update/:sessionId', (request, env) => handleUpdateSession(request, env)) | ||
.patch('/add/:sessionId', (request, env) => handleAddToSession(request, env)) | ||
.all('*', () => new Response('Invalid request', { status: 404 })); | ||
|
||
export default { | ||
fetch: router.handle, | ||
}; | ||
export default { ...router }; // Export the router |
Oops, something went wrong.