Skip to content

Commit

Permalink
chore(config): migrate constants to TS (freeCodeCamp#51485)
Browse files Browse the repository at this point in the history
  • Loading branch information
ojeytonwilliams authored Sep 6, 2023
1 parent 9885d1a commit c071993
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 77 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ config/donation-settings.js
config/superblocks.js
config/superblocks.test.js
config/challenge-types.js
config/constants.js

### Generated utils files ###
utils/index.js
Expand Down
8 changes: 1 addition & 7 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ client/config/browser-scripts/*.json
curriculum/challenges/_meta/*/*
curriculum/challenges/**/*
config/**/*.json
config/i18n.js
config/misc.js
config/certification-settings.js
config/donation-settings.js
config/superblocks.js
config/superblocks.test.js
config/challenge-types.js
config/*.js
utils/index.js
utils/get-lines.js
utils/get-lines.test.js
Expand Down
3 changes: 2 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
"mongodb": "^4.16.0",
"nanoid": "3",
"no-profanity": "^1.4.2",
"nodemon": "2.0.22",
"nodemailer": "^6.9.3",
"nodemon": "2.0.22",
"query-string": "^7.1.3"
},
"description": "The freeCodeCamp.org open-source codebase and curriculum",
"devDependencies": {
"@fastify/type-provider-typebox": "3.5.0",
"@total-typescript/ts-reset": "^0.4.0",
"@types/express-session": "1.17.7",
"@types/jsonwebtoken": "^9.0.2",
"@types/nodemailer": "^6.4.8",
Expand Down
1 change: 1 addition & 0 deletions api/src/reset.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@total-typescript/ts-reset';
3 changes: 1 addition & 2 deletions api/src/routes/settings.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { type FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebox';
import { isProfane } from 'no-profanity';
import { isValidUsername } from '../../../utils/validate';
// we have to use this file as JavaScript because it is used by the old api.
import { blocklistedUsernames } from '../../../config/constants.js';
import { blocklistedUsernames } from '../../../config/constants';
import { schemas } from '../schemas';

/**
Expand Down
26 changes: 26 additions & 0 deletions config/constants.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { blocklistedUsernames } from './constants';

describe('constants', () => {
describe('blocklistedUsernames', () => {
it('should not contain duplicate values', () => {
const uniqueValues = new Set(blocklistedUsernames);
expect(blocklistedUsernames.length).toEqual(uniqueValues.size);
});

it('should contain all the letters in the latin alphabet', () => {
const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
expect(blocklistedUsernames).toEqual(expect.arrayContaining(alphabet));
});
});
});

// Type tests:
type BlocklistedUsernames = (typeof blocklistedUsernames)[number];

type HasString = string extends BlocklistedUsernames ? true : false;

type Expect<T extends true> = T;

// @ts-expect-error - This is intended to fail since we want to ensure that blocklistedUsernames is an array of literals
// eslint-disable-next-line @typescript-eslint/no-unused-vars
type Test = Expect<HasString>;
72 changes: 32 additions & 40 deletions config/constants.js → config/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
let alphabet = '';
const alphabet = [
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z'
] as const;

for (let i = 0; i < 26; i++) {
alphabet += String.fromCharCode(97 + i);
}

const i18nConstants = [
export const i18nConstants = [
// reserved paths for localizations
'afrikaans',
'arabic',
Expand Down Expand Up @@ -37,9 +60,9 @@ const i18nConstants = [
'turkish',
'ukrainian',
'vietnamese'
];
] as const;

let blocklist = [
export const blocklistedUsernames = [
...alphabet,
...i18nConstants,
'about',
Expand Down Expand Up @@ -172,11 +195,9 @@ let blocklist = [
'509',
'510',
'511',
'about',
'about-us',
'abuse',
'access',
'account',
'accounts',
'ad',
'add',
Expand All @@ -199,7 +220,6 @@ let blocklist = [
'alpha',
'amp',
'analytics',
'api',
'app',
'apps',
'asc',
Expand Down Expand Up @@ -242,7 +262,6 @@ let blocklist = [
'channel',
'channels',
'chart',
'chat',
'checkout',
'clear',
'client',
Expand All @@ -260,7 +279,6 @@ let blocklist = [
'contest',
'cookies',
'copy',
'copyright',
'count',
'create',
'crossdomain.xml',
Expand Down Expand Up @@ -307,7 +325,6 @@ let blocklist = [
'error',
'errors',
'event',
'events',
'example',
'exception',
'exit',
Expand All @@ -331,12 +348,10 @@ let blocklist = [
'followers',
'following',
'fonts',
'forgot',
'forgot-password',
'forgotpassword',
'form',
'forms',
'forum',
'forums',
'friend',
'friends',
Expand Down Expand Up @@ -386,12 +401,10 @@ let blocklist = [
'isatap',
'issues',
'it',
'jobs',
'join',
'js',
'json',
'keybase.txt',
'learn',
'legal',
'license',
'licensing',
Expand All @@ -403,8 +416,6 @@ let blocklist = [
'localdomain',
'localhost',
'lock',
'login',
'logout',
'lost-password',
'mail',
'mail0',
Expand All @@ -419,7 +430,6 @@ let blocklist = [
'mail9',
'mailer-daemon',
'mailerdaemon',
'map',
'marketing',
'marketplace',
'master',
Expand All @@ -440,7 +450,6 @@ let blocklist = [
'net',
'network',
'new',
'news',
'newsletter',
'newsletters',
'next',
Expand Down Expand Up @@ -503,13 +512,10 @@ let blocklist = [
'previous',
'pricing',
'print',
'privacy',
'privacy-policy',
'private',
'prod',
'product',
'production',
'profile',
'profiles',
'project',
'projects',
Expand All @@ -529,7 +535,6 @@ let blocklist = [
'report',
'request',
'request-password',
'reset',
'reset-password',
'response',
'return',
Expand All @@ -551,16 +556,12 @@ let blocklist = [
'secure',
'security',
'select',
'services',
'session',
'sessions',
'settings',
'setup',
'share',
'shift',
'shop',
'signin',
'signup',
'site',
'sitemap',
'sites',
Expand Down Expand Up @@ -591,7 +592,6 @@ let blocklist = [
'sudo',
'super',
'superuser',
'support',
'survey',
'sync',
'sysadmin',
Expand All @@ -601,7 +601,6 @@ let blocklist = [
'tags',
'team',
'telnet',
'terms',
'terms-of-use',
'test',
'testimonials',
Expand All @@ -625,12 +624,9 @@ let blocklist = [
'undefined',
'unfollow',
'unlike',
'unsubscribe',
'update',
'upgrade',
'usenet',
'user',
'username',
'users',
'uucp',
'var',
Expand All @@ -644,7 +640,6 @@ let blocklist = [
'website',
'widget',
'widgets',
'wiki',
'wpad',
'write',
'www',
Expand All @@ -657,7 +652,4 @@ let blocklist = [
'yourname',
'yourusername',
'zlib'
];

exports.blocklistedUsernames = [...new Set(blocklist)];
exports.i18nConstants = i18nConstants;
] as const;
Loading

0 comments on commit c071993

Please sign in to comment.