Skip to content

Commit

Permalink
import/export with js extension
Browse files Browse the repository at this point in the history
In order to support ESM, files need to be imported and exported with file extenstions.
While Jest seems to support ESM via hack config, we are using ts-jest, so in order to make file extensions to work, we need a moduleNameMapper configuration that allows to remove the file extension, so jest can resolve properly.
Related links:
kulshekhar/ts-jest#2475
kulshekhar/ts-jest#1057
https://dev.to/antongolub/ts-and-ts-jest-meet-type-module-5chc
  • Loading branch information
joselcvarela committed Oct 4, 2021
1 parent 46f00d6 commit 1c1f261
Show file tree
Hide file tree
Showing 66 changed files with 213 additions and 185 deletions.
9 changes: 9 additions & 0 deletions packages/sdk/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const parentConfig = require('../../.eslintrc.js');

module.exports = {
...parentConfig,
parserOptions: {
...parentConfig.parserOptions,
sourceType: 'module',
},
};
8 changes: 8 additions & 0 deletions packages/sdk/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const parentConfig = require('../../.eslintrc.js');

module.exports = {
...parentConfig,
parserOptions: {
sourceType: 'module',
},
};
8 changes: 6 additions & 2 deletions packages/sdk/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
require('dotenv').config();
import { config as dotenv } from 'dotenv';
dotenv();

module.exports = {
export default {
preset: 'ts-jest',
verbose: true,
setupFiles: ['dotenv/config'],
testURL: process.env.TEST_URL || 'http://localhost',
collectCoverageFrom: ['src/**/*.ts'],
testPathIgnorePatterns: ['dist'],
moduleNameMapper: {
'^@/(.*).js$': '<rootDir>/$1',
},
};
1 change: 1 addition & 0 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"files": [
"dist"
],
"type": "module",
"scripts": {
"prebuild": "npm run cleanup",
"build": "run-p build:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PasswordsHandler } from './handlers/passwords';
import { PasswordsHandler } from '@/src/handlers/passwords.js';

export type AuthCredentials = {
email: string;
Expand Down
10 changes: 5 additions & 5 deletions packages/sdk/src/base/auth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AuthCredentials, AuthLoginOptions, AuthRefreshOptions, AuthResult, AuthToken, IAuth } from '../auth';
import { PasswordsHandler } from '../handlers/passwords';
import { IStorage } from '../storage';
import { ITransport } from '../transport';
import { Debouncer } from '../utils';
import { AuthCredentials, AuthLoginOptions, AuthRefreshOptions, AuthResult, AuthToken, IAuth } from '@/src/auth.js';
import { PasswordsHandler } from '@/src/handlers/passwords.js';
import { IStorage } from '@/src/storage.js';
import { ITransport } from '@/src/transport.js';
import { Debouncer } from '@/src/utils.js';

export type AuthOptions = {
mode?: 'json' | 'cookie';
Expand Down
28 changes: 14 additions & 14 deletions packages/sdk/src/base/directus.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IAuth } from '../auth';
import { IDirectus } from '../directus';
import { IAuth } from '@/src/auth.js';
import { IDirectus } from '@/src/directus.js';
import {
ActivityHandler,
CollectionsHandler,
Expand All @@ -15,18 +15,18 @@ import {
SettingsHandler,
UsersHandler,
UtilsHandler,
} from '../handlers';
import { IItems } from '../items';
import { ITransport } from '../transport';
import { ItemsHandler } from './items';
import { AxiosTransport } from './transport';
import { Auth } from './auth';
import { IStorage } from '../storage';
import { LocalStorage, MemoryStorage } from './storage';
import { TypeMap, TypeOf } from '../types';
import { GraphQLHandler } from '../handlers/graphql';
import { ISingleton } from '../singleton';
import { SingletonHandler } from '../handlers/singleton';
} from '@/src/handlers/index.js';
import { IItems } from '@/src/items.js';
import { ITransport } from '@/src/transport.js';
import { ItemsHandler } from '@/src/base/items.js';
import { AxiosTransport } from '@/src/base/transport/index.js';
import { Auth } from '@/src/base/auth.js';
import { IStorage } from '@/src/storage.js';
import { LocalStorage, MemoryStorage } from '@/src/base/storage/index.js';
import { TypeMap, TypeOf } from '@/src/types.js';
import { GraphQLHandler } from '@/src/handlers/graphql.js';
import { ISingleton } from '@/src/singleton.js';
import { SingletonHandler } from '@/src/handlers/singleton.js';

export type DirectusOptions = {
auth?: IAuth;
Expand Down
10 changes: 5 additions & 5 deletions packages/sdk/src/base/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './storage';
export * from './transport';
export * from '@/src/base/storage/index.js';
export * from '@/src/base/transport/index.js';

export * from './auth';
export * from './directus';
export * from './items';
export * from '@/src/base/auth.js';
export * from '@/src/base/directus.js';
export * from '@/src/base/items.js';
6 changes: 3 additions & 3 deletions packages/sdk/src/base/items.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ITransport } from '../transport';
import { IItems, Item, QueryOne, QueryMany, OneItem, ManyItems, PartialItem } from '../items';
import { ID } from '../types';
import { ITransport } from '@/src/transport.js';
import { IItems, Item, QueryOne, QueryMany, OneItem, ManyItems, PartialItem } from '@/src/items.js';
import { ID } from '@/src/types.js';

export class ItemsHandler<T extends Item> implements IItems<T> {
protected transport: ITransport;
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/base/storage/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IStorage } from '../../storage';
import { IStorage } from '@/src/storage.js';

export abstract class BaseStorage implements IStorage {
get auth_token(): string | null {
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/base/storage/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './base';
export * from './memory';
export * from './localstorage';
export * from '@/src/base/storage/base.js';
export * from '@/src/base/storage/memory.js';
export * from '@/src/base/storage/localstorage.js';
2 changes: 1 addition & 1 deletion packages/sdk/src/base/storage/localstorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseStorage } from './base';
import { BaseStorage } from '@/src/base/storage/base.js';

export class LocalStorage extends BaseStorage {
private prefix: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/base/storage/memory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseStorage } from './base';
import { BaseStorage } from '@/src/base/storage/base.js';

export class MemoryStorage extends BaseStorage {
private prefix: string;
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/base/transport/axios-transport.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IStorage } from '../../storage';
import { IStorage } from '@/src/storage.js';
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios';
import { ITransport, TransportMethods, TransportResponse, TransportError, TransportOptions } from '../../transport';
import { ITransport, TransportMethods, TransportResponse, TransportError, TransportOptions } from '@/src/transport.js';

export type AxiosTransportRefreshHandler = () => Promise<void>;

Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/base/transport/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './axios-transport';
export * from '@/src/base/transport/axios-transport.js';
18 changes: 9 additions & 9 deletions packages/sdk/src/directus.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IAuth } from './auth';
import { IAuth } from '@/src/auth.js';
import {
ActivityHandler,
CollectionsHandler,
Expand All @@ -13,15 +13,15 @@ import {
ServerHandler,
SettingsHandler,
UsersHandler,
} from './handlers';
} from '@/src/handlers/index.js';

import { IItems } from './items';
import { ITransport } from './transport';
import { UtilsHandler } from './handlers/utils';
import { IStorage } from './storage';
import { TypeMap, TypeOf } from './types';
import { GraphQLHandler } from './handlers/graphql';
import { ISingleton } from './singleton';
import { IItems } from '@/src/items.js';
import { ITransport } from '@/src/transport.js';
import { UtilsHandler } from '@/src/handlers/utils.js';
import { IStorage } from '@/src/storage.js';
import { TypeMap, TypeOf } from '@/src/types.js';
import { GraphQLHandler } from '@/src/handlers/graphql.js';
import { ISingleton } from '@/src/singleton.js';

export type DirectusTypes = {
activity: undefined;
Expand Down
8 changes: 4 additions & 4 deletions packages/sdk/src/handlers/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* Activity handler
*/

import { ItemsHandler } from '../base/items';
import { ITransport } from '../transport';
import { ActivityType, DefaultType } from '../types';
import { CommentsHandler } from './comments';
import { ItemsHandler } from '@/src/base/items.js';
import { ITransport } from '@/src/transport.js';
import { ActivityType, DefaultType } from '@/src/types.js';
import { CommentsHandler } from '@/src/handlers/comments.js';

export type ActivityItem<T = DefaultType> = ActivityType & T;

Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/handlers/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* Collections handler
*/

import { ManyItems, OneItem, PartialItem, QueryOne } from '../items';
import { ITransport } from '../transport';
import { CollectionType, DefaultType } from '../types';
import { ManyItems, OneItem, PartialItem, QueryOne } from '@/src/items.js';
import { ITransport } from '@/src/transport.js';
import { CollectionType, DefaultType } from '@/src/types.js';

export type CollectionItem<T = DefaultType> = CollectionType & T;

Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/handlers/comments.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Comment, ID } from '../types';
import { ITransport } from '../transport';
import { ActivityItem } from './activity';
import { Comment, ID } from '@/src/types.js';
import { ITransport } from '@/src/transport.js';
import { ActivityItem } from '@/src/handlers/activity.js';

export class CommentsHandler<T> {
private transport: ITransport;
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/handlers/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Settings handler
*/

import { ITransport, TransportOptions, TransportResponse } from '../transport';
import { ITransport, TransportOptions, TransportResponse } from '@/src/transport.js';

export class ExtensionEndpoint implements ITransport {
private name: string;
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/handlers/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* Fields handler
*/

import { ManyItems, OneItem, PartialItem } from '../items';
import { ITransport } from '../transport';
import { FieldType, DefaultType, ID } from '../types';
import { ManyItems, OneItem, PartialItem } from '@/src/items.js';
import { ITransport } from '@/src/transport.js';
import { FieldType, DefaultType, ID } from '@/src/types.js';

export type FieldItem<T = DefaultType> = FieldType & T;

Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/handlers/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* Files handler
*/

import { ItemsHandler } from '../base/items';
import { ITransport } from '../transport';
import { FileType, DefaultType } from '../types';
import { ItemsHandler } from '@/src/base/items.js';
import { ITransport } from '@/src/transport.js';
import { FileType, DefaultType } from '@/src/types.js';

export type FileItem<T = DefaultType> = FileType & T;

Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/handlers/folders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* Folders handler
*/

import { ItemsHandler } from '../base/items';
import { ITransport } from '../transport';
import { FolderType, DefaultType } from '../types';
import { ItemsHandler } from '@/src/base/items.js';
import { ITransport } from '@/src/transport.js';
import { FolderType, DefaultType } from '@/src/types.js';

export type FolderItem<T = DefaultType> = FolderType & T;

Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/handlers/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ITransport, TransportResponse } from '../transport';
import { ITransport, TransportResponse } from '@/src/transport.js';

export class GraphQLHandler {
private transport: ITransport;
Expand Down
33 changes: 18 additions & 15 deletions packages/sdk/src/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
export * from './activity';
export * from './comments';
export * from './collections';
export * from './fields';
export * from './files';
export * from './folders';
export * from './permissions';
export * from './presets';
export * from './relations';
export * from './revisions';
export * from './roles';
export * from './server';
export * from './settings';
export * from './users';
export * from './utils';
export * from '@/src/handlers/activity.js';
export * from '@/src/handlers/collections.js';
export * from '@/src/handlers/comments.js';
export * from '@/src/handlers/fields.js';
export * from '@/src/handlers/files.js';
export * from '@/src/handlers/folders.js';
export * from '@/src/handlers/invites.js';
export * from '@/src/handlers/me.js';
export * from '@/src/handlers/permissions.js';
export * from '@/src/handlers/presets.js';
export * from '@/src/handlers/relations.js';
export * from '@/src/handlers/revisions.js';
export * from '@/src/handlers/roles.js';
export * from '@/src/handlers/server.js';
export * from '@/src/handlers/settings.js';
export * from '@/src/handlers/tfa.js';
export * from '@/src/handlers/users.js';
export * from '@/src/handlers/utils.js';
4 changes: 2 additions & 2 deletions packages/sdk/src/handlers/invites.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ITransport } from '../transport';
import { ID } from '../types';
import { ITransport } from '@/src/transport.js';
import { ID } from '@/src/types.js';

export class InvitesHandler {
private transport: ITransport;
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/handlers/me.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PartialItem, QueryOne } from '../items';
import { ITransport } from '../transport';
import { TFAHandler } from './tfa';
import { PartialItem, QueryOne } from '@/src/items.js';
import { ITransport } from '@/src/transport.js';
import { TFAHandler } from '@/src/handlers/tfa.js';

export class MeHandler<T> {
private _transport: ITransport;
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/handlers/passwords.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ITransport } from '../transport';
import { ITransport } from '@/src/transport.js';

export class PasswordsHandler {
private transport: ITransport;
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/handlers/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* Permissions handler
*/

import { ItemsHandler } from '../base/items';
import { ITransport } from '../transport';
import { PermissionType, DefaultType } from '../types';
import { ItemsHandler } from '@/src/base/items.js';
import { ITransport } from '@/src/transport.js';
import { PermissionType, DefaultType } from '@/src/types.js';

export type PermissionItem<T = DefaultType> = PermissionType & T;

Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/handlers/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* Presets handler
*/

import { ItemsHandler } from '../base/items';
import { ITransport } from '../transport';
import { PresetType, DefaultType } from '../types';
import { ItemsHandler } from '@/src/base/items.js';
import { ITransport } from '@/src/transport.js';
import { PresetType, DefaultType } from '@/src/types.js';

export type PresetItem<T = DefaultType> = PresetType & T;

Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/handlers/relations.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Relations handler
*/
import { ManyItems, OneItem, PartialItem } from '../items';
import { ITransport } from '../transport';
import { RelationType, DefaultType, ID } from '../types';
import { ManyItems, OneItem, PartialItem } from '@/src/items.js';
import { ITransport } from '@/src/transport.js';
import { RelationType, DefaultType, ID } from '@/src/types.js';

export type RelationItem<T = DefaultType> = RelationType & T;
export class RelationsHandler<T = RelationItem> {
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/handlers/revisions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* Revisions handler
*/

import { ItemsHandler } from '../base/items';
import { ITransport } from '../transport';
import { RevisionType, DefaultType } from '../types';
import { ItemsHandler } from '@/src/base/items.js';
import { ITransport } from '@/src/transport.js';
import { RevisionType, DefaultType } from '@/src/types.js';

export type RevisionItem<T = DefaultType> = RevisionType & T;

Expand Down
Loading

0 comments on commit 1c1f261

Please sign in to comment.