-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* New `/config` route for GET & PUT * Config API supports any key depth * Added fullKey property to daCtx Resolves: GH-21
- Loading branch information
1 parent
02f7188
commit c51fad7
Showing
9 changed files
with
180 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ on: | |
branches: | ||
- main | ||
|
||
|
||
jobs: | ||
action: | ||
if: github.event.pull_request.merged == 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
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,22 @@ | ||
/* | ||
* Copyright 2024 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
import putKv from '../storage/kv/put.js'; | ||
import getKv from '../storage/kv/get.js'; | ||
|
||
export function postConfig({ req, env, daCtx }) { | ||
return putKv(req, env, daCtx); | ||
} | ||
|
||
export function getConfig({ env, daCtx }) { | ||
return getKv(env, daCtx); | ||
} |
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,17 @@ | ||
/* | ||
* Copyright 2024 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
export default async function getKv(env, daCtx) { | ||
const body = await env.DA_CONFIG.get(daCtx.fullKey); | ||
if (body) return { body, status: 200 }; | ||
return { body: JSON.stringify({ error: 'not found' }), status: 404 }; | ||
} |
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,41 @@ | ||
/* | ||
* Copyright 2024 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
async function save(env, key, string) { | ||
let body; | ||
let status; | ||
try { | ||
// Parse it to at least validate its json | ||
JSON.parse(string); | ||
// Put it (seems to not return a response) | ||
await env.DA_CONFIG.put(key, string); | ||
// Validate the content is there | ||
body = await env.DA_CONFIG.get(key); | ||
status = 201; | ||
} catch { | ||
body = JSON.stringify({ error: 'Couldn\'t parse or save config.' }); | ||
status = 400; | ||
} | ||
return { body, status }; | ||
} | ||
|
||
export default async function putKv(req, env, daCtx) { | ||
try { | ||
const formData = await req.formData(); | ||
const config = formData.get('config'); | ||
if (config) return save(env, daCtx.fullKey, config); | ||
} catch { | ||
// eslint-disable-next-line no-console | ||
console.log('No form data'); | ||
} | ||
return { body: JSON.stringify({ error: 'No config or form data.' }), status: 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
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,73 @@ | ||
import assert from 'assert'; | ||
|
||
import getKv from '../../../src/storage/kv/get.js'; | ||
import putKv from '../../../src/storage/kv/put.js'; | ||
|
||
const MOCK_CONFIG = '{"mock":"data"}'; | ||
|
||
describe('KV storage', () => { | ||
describe('Get success', async () => { | ||
const env = { | ||
DA_CONFIG: { | ||
get: () => { return MOCK_CONFIG }, | ||
} | ||
}; | ||
const daCtx = { fullKey: 'adobe/geometrixx' }; | ||
|
||
const resp = await getKv(env, daCtx); | ||
assert.strictEqual(resp.body, MOCK_CONFIG); | ||
assert.strictEqual(resp.status, 200); | ||
}); | ||
|
||
describe('Get not found', async () => { | ||
const env = { DA_CONFIG: { get: () => { return null } } }; | ||
const daCtx = { fullKey: 'adobe/geometrixx' }; | ||
|
||
const resp = await getKv(env, daCtx); | ||
assert.strictEqual(resp.body, '{"error":"not found"}'); | ||
assert.strictEqual(resp.status, 404); | ||
}); | ||
|
||
describe('Put success', async () => { | ||
const formData = new FormData(); | ||
formData.append('config', MOCK_CONFIG); | ||
|
||
const req = { formData: () => { return formData; } }; | ||
const env = { | ||
DA_CONFIG: { | ||
put: () => { return undefined }, | ||
get: () => { return MOCK_CONFIG }, | ||
} | ||
}; | ||
const daCtx = { fullKey: 'adobe/geometrixx' }; | ||
const resp = await putKv(req, env, daCtx); | ||
assert.strictEqual(resp.body, MOCK_CONFIG); | ||
assert.strictEqual(resp.status, 201); | ||
}); | ||
|
||
describe('Put without form data', async () => { | ||
const req = { formData: () => { return null; } }; | ||
const env = {}; | ||
const daCtx = { fullKey: 'adobe/geometrixx' }; | ||
const resp = await putKv(req, env, daCtx); | ||
assert.strictEqual(resp.body, '{"error":"No config or form data."}'); | ||
assert.strictEqual(resp.status, 400); | ||
}); | ||
|
||
describe('Put with malformed config', async () => { | ||
const formData = new FormData(); | ||
formData.append('config', 'abc'); | ||
|
||
const req = { formData: () => { return formData; } }; | ||
const env = { | ||
DA_CONFIG: { | ||
put: () => { return undefined }, | ||
get: () => { return MOCK_CONFIG }, | ||
} | ||
}; | ||
const daCtx = { fullKey: 'adobe/geometrixx' }; | ||
const resp = await putKv(req, env, daCtx); | ||
assert.strictEqual(resp.body, '{"error":"Couldn\'t parse or save config."}'); | ||
assert.strictEqual(resp.status, 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