Skip to content

Commit 0dde84d

Browse files
authored
fix: use extension in relative imports (better ESM compatibility) (#117)
1 parent 4216bc1 commit 0dde84d

31 files changed

+120
-96
lines changed

.eslintrc.cjs

+14-1
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,30 @@ module.exports = {
2020
"plugin:@typescript-eslint/eslint-recommended",
2121
"plugin:@typescript-eslint/recommended",
2222
"plugin:@typescript-eslint/recommended-requiring-type-checking",
23+
"plugin:import/recommended",
24+
"plugin:import/typescript",
2325
"prettier",
2426
"plugin:prettier/recommended"
2527
],
2628

2729
rules: {
2830
"prettier/prettier": "error",
31+
"@typescript-eslint/consistent-type-imports": "error",
2932
"@typescript-eslint/no-empty-interface": [
3033
"error",
3134
{ allowSingleExtends: true }
3235
],
3336
"@typescript-eslint/no-use-before-define": ["error", { functions: false }],
34-
'@typescript-eslint/no-explicit-any': [0]
37+
"@typescript-eslint/no-explicit-any": [0],
38+
"import/no-named-as-default-member": [0],
39+
// Waiting for https://github.com/import-js/eslint-plugin-import/issues/2111
40+
//"import/extensions": ["error", "ignorePackages"]
41+
},
42+
43+
settings: {
44+
"import/resolver": {
45+
typescript: true,
46+
node: true
47+
}
3548
}
3649
};

jest.config.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import type { Config } from '@jest/types';
22

33
const config: Config.InitialOptions = {
4-
preset: 'ts-jest/presets/default-esm',
4+
extensionsToTreatAsEsm: ['.ts'],
55
setupFilesAfterEnv: ['./jest.setup.ts'],
6-
globals: {
7-
'ts-jest': {
8-
useESM: true,
9-
}
6+
moduleNameMapper: {
7+
'^(\\.{1,2}/.*)\\.js$': '$1',
8+
},
9+
transform: {
10+
'^.+\\.ts$': [
11+
'ts-jest',
12+
{
13+
useESM: true,
14+
tsconfig: './tsconfig.esm.json',
15+
},
16+
],
1017
},
1118
};
1219

jest.setup.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import fetch from "jest-fetch-mock";
1+
import jestMock from "jest-fetch-mock";
22

3-
// @ts-ignore
4-
global.fetch = fetch;
3+
jestMock.enableMocks();

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,20 @@
2626
"@types/jest": "^29.0.0",
2727
"@types/jsonld": "^1.5.0",
2828
"@types/lodash.get": "^4.4.0",
29-
"@types/node": "^16.0.0",
29+
"@types/node": "^18.0.0",
3030
"@typescript-eslint/eslint-plugin": "^5.0.0",
3131
"@typescript-eslint/parser": "^5.0.0",
3232
"eslint": "^8.0.0",
3333
"eslint-config-prettier": "^8.0.0",
34+
"eslint-import-resolver-typescript": "^3.5.1",
35+
"eslint-plugin-import": "^2.26.0",
3436
"eslint-plugin-prettier": "^4.0.0",
3537
"eslint-watch": "^8.0.0",
3638
"jest": "^29.0.0",
3739
"jest-fetch-mock": "^3.0.0",
3840
"openapi-types": "^12.0.0",
3941
"prettier": "^2.2.0",
40-
"ts-jest": "^28.0.0",
42+
"ts-jest": "^29.0.0",
4143
"ts-node": "^10.9.0",
4244
"typescript": "^4.1.0"
4345
},

src/Api.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Resource } from "./Resource";
2-
import { assignSealed } from "./utils/assignSealed";
3-
import { Nullable } from "./types";
1+
import { assignSealed } from "./utils/assignSealed.js";
2+
import type { Resource } from "./Resource.js";
3+
import type { Nullable } from "./types.js";
44

55
export interface ApiOptions
66
extends Nullable<{

src/Field.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { assignSealed } from "./utils/assignSealed";
2-
import { Resource } from "./Resource";
3-
import { Nullable } from "./types";
1+
import { assignSealed } from "./utils/assignSealed.js";
2+
import type { Resource } from "./Resource.js";
3+
import type { Nullable } from "./types.js";
44

55
export type FieldType =
66
| "string"

src/Operation.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { assignSealed } from "./utils/assignSealed";
2-
import { Nullable } from "./types";
1+
import { assignSealed } from "./utils/assignSealed.js";
2+
import type { Nullable } from "./types.js";
33

44
export type OperationType = "show" | "edit" | "delete" | "list" | "create";
55

src/Resource.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Field } from "./Field";
2-
import { Operation } from "./Operation";
3-
import { Parameter } from "./Parameter";
4-
import { assignSealed } from "./utils/assignSealed";
5-
import { Nullable } from "./types";
1+
import { assignSealed } from "./utils/assignSealed.js";
2+
import type { Field } from "./Field.js";
3+
import type { Operation } from "./Operation.js";
4+
import type { Parameter } from "./Parameter.js";
5+
import type { Nullable } from "./types.js";
66

77
export interface ResourceOptions
88
extends Nullable<{

src/graphql/fetchQuery.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ExecutionResult } from "graphql";
1+
import type { ExecutionResult } from "graphql";
22

33
const setOptions = (query: string, options: RequestInit): RequestInit => {
44
if (!options.method) {

src/graphql/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { default as fetchQuery } from "./fetchQuery";
2-
export { default as parseGraphQl } from "./parseGraphQl";
1+
export { default as fetchQuery } from "./fetchQuery.js";
2+
export { default as parseGraphQl } from "./parseGraphQl.js";

src/graphql/parseGraphQl.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import {
2-
getIntrospectionQuery,
1+
import { getIntrospectionQuery } from "graphql/utilities";
2+
import fetchQuery from "./fetchQuery.js";
3+
import { Api } from "../Api.js";
4+
import { Field } from "../Field.js";
5+
import { Resource } from "../Resource.js";
6+
import type {
37
IntrospectionObjectType,
48
IntrospectionOutputTypeRef,
59
IntrospectionQuery,
610
} from "graphql/utilities";
7-
import fetchQuery from "./fetchQuery";
8-
import { Api } from "../Api";
9-
import { Field } from "../Field";
10-
import { Resource } from "../Resource";
1111

1212
const getRangeFromGraphQlType = (type: IntrospectionOutputTypeRef): string => {
1313
if (type.kind === "NON_NULL") {

src/hydra/fetchJsonLd.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { FetchMock } from "jest-fetch-mock";
2-
import fetchJsonLd, { ResponseDocument } from "./fetchJsonLd";
1+
import fetchJsonLd from "./fetchJsonLd.js";
2+
import type { FetchMock } from "jest-fetch-mock";
3+
import type { ResponseDocument } from "./fetchJsonLd.js";
34

45
const fetchMock = fetch as FetchMock;
56

src/hydra/fetchJsonLd.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Document, JsonLd, RemoteDocument } from "jsonld/jsonld-spec";
2-
import { RequestInitExtended } from "./types";
1+
import type { Document, JsonLd, RemoteDocument } from "jsonld/jsonld-spec";
2+
import type { RequestInitExtended } from "./types.js";
33

44
const jsonLdMimeType = "application/ld+json";
55

src/hydra/fetchResource.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import get from "lodash.get";
2-
import fetchJsonLd from "./fetchJsonLd";
3-
import type { IriTemplateMapping, RequestInitExtended } from "./types";
2+
import fetchJsonLd from "./fetchJsonLd.js";
3+
import type { IriTemplateMapping, RequestInitExtended } from "./types.js";
44

55
export default (
66
resourceUrl: string,

src/hydra/getParameters.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Parameter } from "../Parameter";
2-
import { Resource } from "../Resource";
3-
import fetchResource from "./fetchResource";
4-
import { RequestInitExtended } from "./types";
1+
import { Parameter } from "../Parameter.js";
2+
import fetchResource from "./fetchResource.js";
3+
import type { Resource } from "../Resource.js";
4+
import type { RequestInitExtended } from "./types.js";
55

66
export default (
77
resource: Resource,

src/hydra/getType.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FieldType } from "../Field";
1+
import type { FieldType } from "../Field.js";
22

33
const getType = (id: string, range: string): FieldType => {
44
switch (id) {

src/hydra/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export { default as fetchJsonLd } from "./fetchJsonLd";
1+
export { default as fetchJsonLd } from "./fetchJsonLd.js";
22
export {
33
default as parseHydraDocumentation,
44
getDocumentationUrlFromHeaders,
5-
} from "./parseHydraDocumentation";
5+
} from "./parseHydraDocumentation.js";

src/hydra/parseHydraDocumentation.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { FetchMock, MockParams } from "jest-fetch-mock";
2-
import parseHydraDocumentation from "./parseHydraDocumentation";
3-
import { Api } from "../Api";
4-
import parsedJsonReplacer from "../utils/parsedJsonReplacer";
1+
import parseHydraDocumentation from "./parseHydraDocumentation.js";
2+
import parsedJsonReplacer from "../utils/parsedJsonReplacer.js";
3+
import type { FetchMock, MockParams } from "jest-fetch-mock";
4+
import type { Api } from "../Api.js";
55

66
const fetchMock = fetch as FetchMock;
77

src/hydra/parseHydraDocumentation.ts

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import jsonld from "jsonld";
22
import get from "lodash.get";
3-
import { Api } from "../Api";
4-
import { Field } from "../Field";
5-
import { Resource } from "../Resource";
6-
import { Operation, OperationType } from "../Operation";
7-
import { Parameter } from "../Parameter";
8-
import fetchJsonLd from "./fetchJsonLd";
9-
import getParameters from "./getParameters";
10-
import getType from "./getType";
11-
import {
3+
import { Api } from "../Api.js";
4+
import { Field } from "../Field.js";
5+
import { Resource } from "../Resource.js";
6+
import { Operation } from "../Operation.js";
7+
import fetchJsonLd from "./fetchJsonLd.js";
8+
import getParameters from "./getParameters.js";
9+
import getType from "./getType.js";
10+
import type { OperationType } from "../Operation.js";
11+
import type { Parameter } from "../Parameter.js";
12+
import type {
1213
ExpandedClass,
1314
ExpandedDoc,
1415
Entrypoint,
1516
ExpandedRdfProperty,
1617
RequestInitExtended,
17-
} from "./types";
18+
} from "./types.js";
1819

1920
/**
2021
* Extracts the short name of a resource.

src/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
export * from "./Api";
2-
export * from "./Field";
3-
export * from "./Operation";
4-
export * from "./Parameter";
5-
export * from "./Resource";
1+
export * from "./Api.js";
2+
export * from "./Field.js";
3+
export * from "./Operation.js";
4+
export * from "./Parameter.js";
5+
export * from "./Resource.js";
66
export * from "./graphql";
77
export * from "./hydra";
88
export * from "./openapi3";

src/openapi3/getType.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import inflection from "inflection";
2-
import { FieldType } from "../Field";
2+
import type { FieldType } from "../Field.js";
33

44
const getType = (openApiType: string, format?: string): FieldType => {
55
if (format) {

src/openapi3/handleJson.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { OpenAPIV3 } from "openapi-types";
2-
import handleJson from "./handleJson";
3-
import parsedJsonReplacer from "../utils/parsedJsonReplacer";
1+
import handleJson from "./handleJson.js";
2+
import parsedJsonReplacer from "../utils/parsedJsonReplacer.js";
3+
import type { OpenAPIV3 } from "openapi-types";
44

55
const openApi3Definition: OpenAPIV3.Document = {
66
openapi: "3.0.2",

src/openapi3/handleJson.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { OpenAPIV3 } from "openapi-types";
21
import { parse as dereference } from "jsonref";
32
import get from "lodash.get";
43
import inflection from "inflection";
5-
import { Field } from "../Field";
6-
import { Operation, OperationType } from "../Operation";
7-
import { Parameter } from "../Parameter";
8-
import { Resource } from "../Resource";
9-
import getResourcePaths from "../utils/getResources";
10-
import getType from "./getType";
4+
import { Field } from "../Field.js";
5+
import { Operation } from "../Operation.js";
6+
import { Parameter } from "../Parameter.js";
7+
import { Resource } from "../Resource.js";
8+
import getResourcePaths from "../utils/getResources.js";
9+
import getType from "./getType.js";
10+
import type { OpenAPIV3 } from "openapi-types";
11+
import type { OperationType } from "../Operation.js";
1112

1213
const isRef = <T>(maybeRef: T | OpenAPIV3.ReferenceObject): maybeRef is T =>
1314
!("$ref" in maybeRef);

src/openapi3/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { default as parseOpenApi3Documentation } from "./parseOpenApi3Documentation";
2-
export * from "./parseOpenApi3Documentation";
1+
export { default as parseOpenApi3Documentation } from "./parseOpenApi3Documentation.js";
2+
export * from "./parseOpenApi3Documentation.js";

src/openapi3/parseOpenApi3Documentation.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { OpenAPIV3 } from "openapi-types";
2-
import { Api } from "../Api";
3-
import handleJson, { removeTrailingSlash } from "./handleJson";
1+
import { Api } from "../Api.js";
2+
import handleJson, { removeTrailingSlash } from "./handleJson.js";
3+
import type { OpenAPIV3 } from "openapi-types";
44

55
export interface ParsedOpenApi3Documentation {
66
api: Api;

src/swagger/handleJson.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { OpenAPIV2 } from "openapi-types";
2-
import { Field } from "../Field";
3-
import handleJson from "./handleJson";
1+
import handleJson from "./handleJson.js";
2+
import type { OpenAPIV2 } from "openapi-types";
3+
import type { Field } from "../Field.js";
44

55
const swaggerApiDefinition: OpenAPIV2.Document = {
66
swagger: "2.0",

src/swagger/handleJson.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { OpenAPIV2 } from "openapi-types";
21
import get from "lodash.get";
32
import inflection from "inflection";
4-
import { Field } from "../Field";
5-
import { Resource } from "../Resource";
6-
import getResourcePaths from "../utils/getResources";
7-
import getType from "../openapi3/getType";
3+
import { Field } from "../Field.js";
4+
import { Resource } from "../Resource.js";
5+
import getResourcePaths from "../utils/getResources.js";
6+
import getType from "../openapi3/getType.js";
7+
import type { OpenAPIV2 } from "openapi-types";
88

99
export const removeTrailingSlash = (url: string): string => {
1010
if (url.endsWith("/")) {

src/swagger/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { default as parseSwaggerDocumentation } from "./parseSwaggerDocumentation";
2-
export * from "./parseSwaggerDocumentation";
1+
export { default as parseSwaggerDocumentation } from "./parseSwaggerDocumentation.js";
2+
export * from "./parseSwaggerDocumentation.js";

src/swagger/parseSwaggerDocumentation.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { OpenAPIV2 } from "openapi-types";
2-
import { Api } from "../Api";
3-
import handleJson, { removeTrailingSlash } from "./handleJson";
1+
import { Api } from "../Api.js";
2+
import handleJson, { removeTrailingSlash } from "./handleJson.js";
3+
import type { OpenAPIV2 } from "openapi-types";
44

55
export interface ParsedSwaggerDocumentation {
66
api: Api;

src/utils/getResources.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import getResourcePaths from "./getResources";
1+
import getResourcePaths from "./getResources.js";
22

33
test("should get resource paths", () => {
44
const paths = {

src/utils/getResources.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { OpenAPIV2, OpenAPIV3 } from "openapi-types";
1+
import type { OpenAPIV2, OpenAPIV3 } from "openapi-types";
22

33
const getResources = (
44
paths: OpenAPIV2.PathsObject | OpenAPIV3.PathsObject

0 commit comments

Comments
 (0)