-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
119 additions
and
0 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
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,46 @@ | ||
import test from 'ava' | ||
import { NewNamespace, Request, Response } from '../test-utils' | ||
import { addPersistedQuery } from './add-persisted-query' | ||
import { getPersistedQuery } from './get-persisted-query' | ||
import { deletePersistedQuery } from './delete-persisted-query' | ||
|
||
test.serial('Should delete PQ from KV', async (t) => { | ||
NewNamespace({ | ||
name: 'PERSISTED_QUERIES', | ||
}) | ||
|
||
let req = Request('POST', '', { key: '123', query: 'query' }) | ||
let res = Response() | ||
await addPersistedQuery(req, res) | ||
t.is(res.statusCode, 200) | ||
|
||
req = Request('GET', 'key=123') | ||
res = Response() | ||
await getPersistedQuery(req, res) | ||
t.is(res.statusCode, 200) | ||
t.deepEqual(res.body as any, { | ||
success: true, | ||
data: 'query', | ||
}) | ||
|
||
req = Request('DELETE', '', { key: '123' }) | ||
res = Response() | ||
await deletePersistedQuery(req, res) | ||
t.is(res.statusCode, 200) | ||
|
||
req = Request('GET', 'key=123') | ||
res = Response() | ||
await getPersistedQuery(req, res) | ||
t.is(res.statusCode, 404) | ||
}) | ||
|
||
test.serial('Should return 400 when key was not provided', async (t) => { | ||
NewNamespace({ | ||
name: 'PERSISTED_QUERIES', | ||
}) | ||
|
||
const req = Request('DELETE', '', {}) | ||
const res = Response() | ||
await deletePersistedQuery(req, res) | ||
t.is(res.statusCode, 400) | ||
}) |
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,42 @@ | ||
import type { Handler } from 'worktop' | ||
import { object, size, string, validate } from 'superstruct' | ||
import { remove as removePQ } from '../repositories/PersistedQueries' | ||
|
||
interface DeletePQRequest { | ||
key: string | ||
query: string | ||
} | ||
|
||
const deleteRequest = object({ | ||
key: size(string(), 1, 100), | ||
}) | ||
|
||
/** | ||
* Deletes persisted query from KV Storage | ||
* | ||
* @param req | ||
* @param res | ||
*/ | ||
export const deletePersistedQuery: Handler = async function (req, res) { | ||
const requestBody = await req.body<DeletePQRequest>() | ||
const [error, input] = validate(requestBody, deleteRequest) | ||
if (!input || error) { | ||
return res.send(400, { | ||
success: false, | ||
error: error?.message, | ||
}) | ||
} | ||
|
||
const result = await removePQ(input.key) | ||
|
||
if (!result) { | ||
return res.send(404, { | ||
success: false, | ||
error: 'Could not delete persisted query', | ||
}) | ||
} | ||
|
||
res.send(200, { | ||
success: true, | ||
}) | ||
} |
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