Skip to content

Commit

Permalink
Merge pull request #225 from pulsar-edit/add-filter-support
Browse files Browse the repository at this point in the history
Add filter support to `/api/packages/search`
  • Loading branch information
confused-Techie authored Jan 13, 2024
2 parents 94648cf + a6044af commit e3adb49
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"start:dev": "cross-env PULSAR_STATUS=dev node ./src/dev_server.js",
"lint": "prettier --check -u -w .",
"complex": "cr --newmi --config .complexrc .",
"js-docs": "jsdoc2md -c ./jsdoc.conf.js ./src/*.js ./src/handlers/*.js ./docs/resources/jsdoc_typedef.js > ./docs/reference/Source_Documentation.md",
"js-docs": "jsdoc2md -c ./jsdoc.conf.js ./src/*.js ./src/controllers/*.js ./src/query_parameters/*.js ./docs/resources/jsdoc_typedef.js > ./docs/reference/Source_Documentation.md",
"contributors:add": "all-contributors add",
"migrations": "pg-migrations apply --directory ./scripts/migrations",
"ignore": "compactignore",
Expand Down
17 changes: 13 additions & 4 deletions src/controllers/getPackagesSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@

module.exports = {
docs: {
summary: "Searches all packages."
summary: "Searches all packages.",
responses: {
200: {
description: "Any array of packages.",
content: {
"application/json": "$packageObjectShortArray"
}
}
}
},
endpoint: {
method: "GET",
Expand All @@ -20,7 +28,8 @@ module.exports = {
sort: (context, req) => { return context.query.sort(req); },
page: (context, req) => { return context.query.page(req); },
direction: (context, req) => { return context.query.direction(req); },
query: (context, req) => { return context.query.query(req); }
query: (context, req) => { return context.query.query(req); },
filter: (context, req) => { return context.query.filter(req); }
},

/**
Expand All @@ -43,10 +52,10 @@ module.exports = {
params.query,
params.page,
params.direction,
params.sort
params.sort,
(params.filter === "theme")
);


if (!packs.ok) {
if (packs.short === "not_found") {
// Because getting not found from the search, means the users
Expand Down
34 changes: 34 additions & 0 deletions src/query_parameters/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module.exports = {
schema: {
name: "filter",
in: "query",
schema: {
type: "string",
enum: [
"package",
"theme"
],
default: "pacakge"
},
required: false,
allowEmptyValue: false,
example: "package",
description: "Deprecated method to display packages or themes. Use `/api/themes/search` or `/api/packages/search` instead."
},
logic: (req) => {
const def = "package";
const valid = [ "theme", "package" ];

const prov = req.query.filter;

if (typeof prov !== "string") {
return def;
}

if (!valid.includes(prov)) {
return def;
}

return prov;
}
};
3 changes: 3 additions & 0 deletions src/query_parameters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const auth = require("./auth.js");
const direction = require("./direction.js");
const engine = require("./engine.js");
const fileExtension = require("./fileExtension.js");
const filter = require("./filter.js");
const login = require("./login.js");
const owner = require("./owner.js");
const packageName = require("./packageName.js");
Expand All @@ -29,6 +30,7 @@ module.exports = {
direction: direction.logic,
engine: engine.logic,
fileExtension: fileExtension.logic,
filter: filter.logic,
login: login.logic,
owner: owner.logic,
packageName: packageName.logic,
Expand All @@ -48,6 +50,7 @@ module.exports = {
direction: direction.schema,
engine: engine.schema,
fileExtension: fileExtension.schema,
filter: filter.schema,
login: login.schema,
owner: owner.schema,
packageName: packageName.schema,
Expand Down
98 changes: 98 additions & 0 deletions tests/http/getPackagesSearch.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
const endpoint = require("../../src/controllers/getPackagesSearch.js");
const database = require("../../src/database.js");
const context = require("../../src/context.js");

describe("Behaves as expected", () => {

beforeAll(async () => {
await database.insertNewPackage({
name: "get-packages-search-theme-test",
repository: {
url: "https://github.com/getPackagesSearch/get-packages-search-theme-test",
type: "git"
},
owner: "getPackagesSearch",
creation_method: "Test Package",
releases: {
latest: "1.0.0"
},
readme: "This is a readme!",
metadata: {
name: "get-packages-search-theme-test",
theme: "syntax"
},
versions: {
"1.0.0": {
dist: {
tarball: "download-url",
sha: "1234"
},
name: "get-packages-search-theme-test"
}
}
});

await database.insertNewPackage({
name: "get-packages-search-test",
repository: {
url: "https://github.com/getPackagesSearch/get-packages-search-test",
type: "git"
},
owner: "getPackagesSearch",
creation_method: "Test Package",
releases: {
latest: "1.0.0"
},
readme: "This is a readme!",
metadata: {
name: "get-packages-search-test"
},
versions: {
"1.0.0": {
dist: {
tarball: "download-url",
sha: "1234"
},
name: "get-packages-search-test"
}
}
});

});

afterAll(async () => {
await database.removePackageByName("get-packages-search-theme-test");
await database.removePackageByName("get-packages-search-test");
});

test("Successfully searches", async () => {
let sso = await endpoint.logic({
sort: "downloads",
page: 1,
direction: "desc",
query: "get packages search",
filter: "package"
}, context);

expect(sso.ok).toBe(true);
expect(sso.content.length).toBe(2);
expect(sso.content[0].name).toBe("get-packages-search-test");
expect(sso.content[1].name).toBe("get-packages-search-theme-test");
expect(sso).toMatchEndpointSuccessObject(endpoint);
});

test("Successfully searches and filters by theme", async () => {
let sso = await endpoint.logic({
sort: "downloads",
page: 1,
direction: "desc",
query: "get packages search",
filter: "theme"
}, context);

expect(sso.ok).toBe(true);
expect(sso.content.length).toBe(1);
expect(sso.content[0].name).toBe("get-packages-search-theme-test");
expect(sso).toMatchEndpointSuccessObject(endpoint);
});
});

0 comments on commit e3adb49

Please sign in to comment.