Skip to content

Commit 7ce80b1

Browse files
committed
fix: CORS headers for public api
1 parent a3403f3 commit 7ce80b1

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/api/countDownload.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Refer https://json-schema.org/understanding-json-schema/index.html
22
import {EXTENSIONS_DETAILS_TABLE, FIELD_EXTENSION_ID} from "../constants.js";
33
import db from "../db.js";
4+
import {HTTP_STATUS_CODES} from "@aicore/libcommonutils";
45

56
const schema = {
67
schema: {
@@ -70,14 +71,18 @@ function _isPublishedVersion(registryPKGJSON, version) {
7071
return versions.includes(version);
7172
}
7273

73-
export async function countDownload(request, _reply) {
74+
export async function countDownload(request, reply) {
7475
const extensionName = request.query.extensionName; // extension.name
7576
const extensionVersion = request.query.extensionVersion; // 1.0.2
77+
reply.header("Access-Control-Allow-Origin", "*");
78+
reply.header("Access-Control-Allow-Methods", "GET");
7679
let registryEntry = await _getRegistryPkgJSON(extensionName);
7780
if(!registryEntry) {
81+
reply.status(HTTP_STATUS_CODES.BAD_REQUEST);
7882
throw new Error("No such extension");
7983
}
8084
if(!_isPublishedVersion(registryEntry.registryPKGJSON, extensionVersion)) {
85+
reply.status(HTTP_STATUS_CODES.BAD_REQUEST);
8186
throw new Error("No such extension version");
8287
}
8388
let status = await db.mathAdd(EXTENSIONS_DETAILS_TABLE, registryEntry.existingRegistryDocumentId, {

test/unit/data/simple-request.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ export function getSimpleGetReply() {
7474
reply.status = function (code) {
7575
reply.statusCode = code;
7676
}
77+
reply.header = function (key, val) {
78+
reply.headers = reply.headers || {};
79+
reply.headers[key] = val;
80+
}
7781
return reply;
7882
}
7983

0 commit comments

Comments
 (0)