Skip to content

Commit

Permalink
TS: Migrate to nodenext moduleResolution, check libs
Browse files Browse the repository at this point in the history
Fixes error "'resolution-mode' assertions are only supported when `moduleResolution` is `node16` or `nodenext`" with `got`. Details: <sindresorhus/got#2051>

Also enable library checks for TypeScript validation now that typings are improved.

Native ESM support details: <https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#esm-nodejs>

Requires using `.js` extensions for relative imports.

`got` is also now ESM-only -> fix import path.

Something's broken with `jwt-decode`'s exports, so using `.default` is required (ref: <auth0/jwt-decode#103> and <auth0/jwt-decode#130>).
  • Loading branch information
mikkopiu committed Jan 10, 2023
1 parent 634e935 commit 585da29
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 93 deletions.
8 changes: 8 additions & 0 deletions .mocharc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
require: "ts-node/register",
extension: ["ts"],
"node-option": ["experimental-specifier-resolution=node", "loader=ts-node/esm"],
spec: ["tests/**/*.spec.ts"],
recursive: true,
exit: true
}
15 changes: 0 additions & 15 deletions .mocharc.json

This file was deleted.

13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"urlify": "^0.3.6"
},
"devDependencies": {
"@types/chai": "^4.3.4",
"@types/mocha": "^10.0.1",
"@types/node": "^18.0.0",
"@typescript-eslint/eslint-plugin": "^5.17.0",
Expand Down
6 changes: 3 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ExpiryMap from 'expiry-map';
import { Got } from 'got/dist/source';
import { Got } from 'got';
import memoize from 'p-memoize';

import {
Expand All @@ -21,7 +21,7 @@ import {
ProjectPipeline,
SearchResult,
SearchSkillResult,
} from './types';
} from './types.js';

/**
* Prefer using the Service instead of this class directly
Expand Down Expand Up @@ -224,7 +224,7 @@ export class Api {
throw new Error(`Found too many (hits ${search.hits})`);
}

const id = search.result[0].companyUserId;
const id = search.result[0]?.companyUserId;

if (id === null || id === undefined) {
throw new Error(`User is missing companyUserId: ${email}`);
Expand Down
4 changes: 3 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ const limiter = new Bottleneck({

function isValidJwtToken(token: string): boolean {
try {
return moment.unix(jwt_decode<JwtPayload>(token).exp || 0).isAfter();
return moment
.unix(jwt_decode.default<JwtPayload>(token).exp || 0)
.isAfter();
} catch (e) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProjectPipelineStage, ProjectState } from './types';
import { ProjectPipelineStage, ProjectState } from './types.js';

const projectStateToHumanReadable = (
value: ProjectState | undefined | null
Expand Down
8 changes: 4 additions & 4 deletions src/service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ExpiryMap from 'expiry-map';
import memoize from 'p-memoize';

import { Api } from './api';
import { Api } from './api.js';
import type {
Company,
CompanyUser,
Expand All @@ -17,15 +17,15 @@ import type {
ProjectTeam,
UserFilter,
WithProfile,
} from './types';
import { getImageUrl } from './urls';
} from './types.js';
import { getImageUrl } from './urls.js';
import {
dropByEmail,
hasActiveRole,
isActiveProject,
onlyActivePeople,
onlyInTeams,
} from './utils';
} from './utils.js';

const ignoreError = () => undefined;

Expand Down
4 changes: 2 additions & 2 deletions src/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
HasProject,
ProjectAssignmentBase,
ProjectBase,
} from './types';
} from './types.js';

// Warning: This was reverse engineered how they build the url
// so there might be cases where this produces invalid url
Expand Down Expand Up @@ -36,7 +36,7 @@ export const isProjectUrl = (url: string): boolean =>

export const parseProjectUrl = (url: string): number => {
const match = PROJECT_URL_REGEX.exec(url);
if (match) {
if (match?.[1]) {
return +match[1];
}
console.log(match, url);
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import moment from 'moment';
import {
projectStageToHumanReadable,
projectStateToHumanReadable,
} from './model';
} from './model.js';
import {
AbsencePeriodDto,
CompanyUser,
Expand All @@ -21,7 +21,7 @@ import {
ProjectState,
TimeSpan,
WithProfile,
} from './types';
} from './types.js';

const latestChangeFirst = (
a: CompanyUserProfileSkillHistory,
Expand All @@ -40,7 +40,7 @@ export const findLatestChangeDate = (
return null;
}

return skill.changeHistory.sort(latestChangeFirst)[0].changeDateTime ?? null;
return skill.changeHistory.sort(latestChangeFirst)[0]?.changeDateTime ?? null;
};

const descendingBySkillLevel = (
Expand Down
2 changes: 1 addition & 1 deletion tests/test-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Project,
ProjectAssignmentBase,
ProjectState,
} from '../src/types';
} from '../src/types.js';

export const company = (props?: Partial<Company>): Company => ({
id: 0,
Expand Down
2 changes: 1 addition & 1 deletion tests/testdata.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CompanyUserProfileSkill } from '../src/types';
import { CompanyUserProfileSkill } from '../src/types.js';

export const versionControlSkill: CompanyUserProfileSkill = {
profileId: 93750,
Expand Down
6 changes: 3 additions & 3 deletions tests/urls.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import 'mocha';

import { expect } from 'chai';

import { HasProject, ProjectAssignmentBase } from '../src/types';
import { HasProject, ProjectAssignmentBase } from '../src/types.js';
import {
buildProjectUrlFromAssignment,
getImageUrl,
isProjectUrl,
parseProjectUrl,
} from '../src/urls';
import { assignment, company, project } from './test-builder';
} from '../src/urls.js';
import { assignment, company, project } from './test-builder.js';

describe('cinode utils', () => {
const TEST_COMPANY_NAME = 'foobarbaz';
Expand Down
8 changes: 4 additions & 4 deletions tests/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'mocha';
import { expect } from 'chai';
import moment from 'moment';

import { ProjectState } from '../src/types';
import { ProjectState } from '../src/types.js';
import {
dropByEmail,
employmentStarted,
Expand All @@ -15,9 +15,9 @@ import {
notUpdatedSince,
onlyInTeams,
resolveSkillName,
} from '../src/utils';
import { assignment, project, skill, user } from './test-builder';
import { versionControlSkill } from './testdata';
} from '../src/utils.js';
import { assignment, project, skill, user } from './test-builder.js';
import { versionControlSkill } from './testdata.js';

describe('cinode utils', () => {
context('hasActiveRole', () => {
Expand Down
73 changes: 18 additions & 55 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,33 @@
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "ES2020" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "es2020" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
// "lib": [], /* Specify library files to be included in the compilation. */
"allowJs": false, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
"declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./dist/", /* Redirect output structure to the directory. */
"target": "ES2020" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ES2020'. */,
"module": "es2020" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"allowJs": false, /* Allow javascript files to be compiled. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
"declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"outDir": "./dist/", /* Redirect output structure to the directory. */
"rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true /* Enable all strict type-checking options. */,
"noImplicitAny": false /* Raise error on expressions and declarations with an implied 'any' type. */,
"strictNullChecks": true /* Enable strict null checks. */,
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
"strict": true /* Enable all strict type-checking options. */,
"noImplicitAny": false /* Raise error on expressions and declarations with an implied 'any' type. */,
"strictNullChecks": true /* Enable strict null checks. */,

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
"noUnusedLocals": true, /* Report errors on unused locals. */
"noUnusedParameters": true, /* Report errors on unused parameters. */
"noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
"noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */

/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
"moduleResolution": "node16", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,

/* Advanced Options */
"skipLibCheck": true /* Skip type checking of declaration files. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
"skipLibCheck": false /* Skip type checking of declaration files. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"include": ["src/**/*.ts", "tests/**/*.ts"],
"exclude": ["node_modules/", "dist/"]
Expand Down

0 comments on commit 585da29

Please sign in to comment.