Skip to content

Commit 396d03d

Browse files
committed
fix: added basic search functionality
1 parent 25dc1e9 commit 396d03d

File tree

11 files changed

+104
-5
lines changed

11 files changed

+104
-5
lines changed

assets/jscpd-badge.svg

Lines changed: 2 additions & 2 deletions
Loading

k8s/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apiVersion: v1
22
description: A Helm chart for kube-ts-server
33
name: kube-ts-server
44
version: 1.0.0
5-
appVersion: 1.5.0
5+
appVersion: 1.5.2
66
home: https://cloud.docker.com/u/kubejs/repository/docker/kubejs/kube-ts-server
77
icon: https://avatars2.githubusercontent.com/u/47761918?s=200&v=4
88
sources:

k8s/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ replicaCount: 2
66

77
image:
88
repository: kubejs/kube-ts-server
9-
tag: 1.5.0
9+
tag: 1.5.2
1010
pullPolicy: Always
1111
containerPort: 3000
1212

src/config/subconfigs/http/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
RATE_LIMITER_SKIP_METHODS,
1414
RATE_LIMITER_WINDOW_MS,
1515
READINESS_CHECK_URL,
16+
SEARCH_QUERY_PARAM_NAME,
1617
TOO_MANY_REQUEST_MESSAGE,
1718
TRUST_PROXY,
1819
VERSION_CHECK_URL,
@@ -72,6 +73,7 @@ export interface ClientConfig {
7273
readonly verifyTokenQueryParamName: string;
7374
readonly resetPasswordUrl: string;
7475
readonly resetPasswordTokenQueryParamName: string;
76+
readonly searchQueryParamName: string;
7577
}
7678

7779
export interface HttpConfig {
@@ -101,6 +103,7 @@ const config: HttpConfig = {
101103
process.env.CLIENT_RESET_PASSWORD_URL,
102104
CLIENT_RESET_PASSWORD_URL
103105
),
106+
searchQueryParamName: getStringValue(process.env.SEARCH_QUERY_PARAM_NAME, SEARCH_QUERY_PARAM_NAME),
104107
siteUrl: getStringValue(process.env.CLIENT_URL, CLIENT_URL),
105108
verifyEmailUrl: getStringValue(
106109
process.env.CLIENT_VERIFY_EMAIL_URL,

src/constants/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const CLIENT_VERIFY_EMAIL_URL = 'http://localhost:9000/verify'; // @note:
1212
export const CLIENT_VERIFY_TOKEN_QUERY_PARAM_NAME = 'token';
1313
export const CLIENT_RESET_PASSWORD_URL = 'http://localhost:9000/reset-password'; // @note: this is client url
1414
export const CLIENT_RESET_PASSWORD_TOKEN_QUERY_PARAM_NAME = 'token';
15+
export const SEARCH_QUERY_PARAM_NAME = 'q';
1516

1617
export const TOO_MANY_REQUEST_MESSAGE =
1718
'Too many accounts created from this IP, please try again after an 10 minutes';
@@ -45,6 +46,7 @@ export const LOGGLY_SUBDOMAIN = 'your-loggly-subdomain';
4546
// MODEL
4647
export const UUID_LENGTH = 36;
4748
export const VARCHAR_LENGTH = 255;
49+
export const SAFE_URL_LENGTH = 2000;
4850
export const TEXT_LENGTH = 65535;
4951
export const KNEX_CLIENT = 'mysql';
5052
export const KNEX_DATABASE = 'test_db';

src/constants/routes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ export const MODULES = '/modules';
2626
export const UNITS = '/units';
2727
export const COMMENTS = '/comments';
2828
export const DISCOVERY_ITEMS = '/discovery-items';
29+
export const SEARCH = '/search';

src/presenter/express/api/v1/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
PERMISSIONS,
1111
ROLES,
1212
ROOT,
13+
SEARCH,
1314
USERS,
1415
} from '../../../../constants/routes';
1516
import Config from '../../presenterFactory/Config';
@@ -20,6 +21,7 @@ import enrolmentsFactory from './routes/enrolments/factory';
2021
import getDiscoveryItems from './routes/getDiscoveryItems';
2122
import permissionsFactory from './routes/permissions/factory';
2223
import rolesFactory from './routes/roles/factory';
24+
import searchHandler from './routes/searchHandler';
2325
import usersFactory from './routes/users/factory';
2426
import describeApi from './routes/utils/describeApi';
2527

@@ -41,6 +43,7 @@ const apiV1 = (config: Config): Router => {
4143
router.use(COURSES, coursesFactory(config));
4244
router.use(ENROLMENTS, enrolmentsFactory(config));
4345
router.use(DISCOVERY_ITEMS, getDiscoveryItems(config));
46+
router.get(SEARCH, searchHandler(config));
4447
// router.use(MODULES, modulesFactory(config));
4548
// router.use(UNITS, unitsFactory(config));
4649
// TODO: add route for uploading /avatar
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import sendResponse from '@js-items/express/dist/utils/sendResponse';
2+
import { toSnake } from 'convert-keys';
3+
import { OK } from 'http-status-codes';
4+
import Record from 'rulr/Record';
5+
import validateData from 'rulr/validateData';
6+
import { SAFE_URL_LENGTH } from '../../../../../../constants';
7+
import String from '../../../../../../utils/validation/rules/String';
8+
import Config from '../../../../presenterFactory/Config';
9+
import catchErrors from '../../../../utils/errors/catchErrors';
10+
11+
12+
const searchHandler = (config: Config) =>
13+
catchErrors(config, async (req, res) => {
14+
const name = config.appConfig.http.client.searchQueryParamName;
15+
16+
const validationSchema = {
17+
[name]: String(0, SAFE_URL_LENGTH),
18+
};
19+
20+
const rules = Record(validationSchema);
21+
const value = req.query[name];
22+
23+
const payload: any = {[name]:value};
24+
25+
validateData(rules)(payload);
26+
27+
const response = await config.service.search({
28+
query: value
29+
});
30+
31+
sendResponse({
32+
body: toSnake(response),
33+
req,
34+
res,
35+
status: OK,
36+
});
37+
});
38+
39+
export default searchHandler;

src/service/factory.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import getDiscoveryItemsForHomepage from './functions/getDiscoveryItemsForHomepa
66
import hasPermission from './functions/hasPermission';
77
import assignRolePermission from './functions/roles/assignRolePermission';
88
import revokeRolePermission from './functions/roles/revokeRolePermission';
9+
import search from './functions/search';
910
import assignUserRole from './functions/users/assignUserRole';
1011
import revokeUserRole from './functions/users/revokeUserRole';
1112

@@ -29,6 +30,7 @@ export default (config: FactoryConfig) => ({
2930
revokeUserRole: revokeUserRole(config),
3031
rolePermission: config.repo.rolePermission,
3132
roles: config.repo.roles,
33+
search: search(config),
3234
sendEmail: config.repo.sendEmail,
3335
userRole: config.repo.userRole,
3436
users: config.repo.users,

src/service/functions/getCourseDetails/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Filter, ItemNotFoundError } from '@js-items/foundation';
22
import _pluck from 'ramda/src/pluck';
33
import Course from '../../../types/items/Course';
4+
import getVisibleUserProperties from '../../../utils/helpers/model/getVisibleUserProperties';
45
import Config from '../../FactoryConfig';
56

67
export interface Options {
@@ -30,7 +31,7 @@ export default ({ repo }: Config) => async ({ filter }: Options) => {
3031
course: {
3132
...course,
3233
category,
33-
user,
34+
user: getVisibleUserProperties(user),
3435
},
3536
};
3637
};

0 commit comments

Comments
 (0)