Skip to content

Commit

Permalink
Merge pull request #19 from shelfio/feature/typescript-support
Browse files Browse the repository at this point in the history
Added support for typescript
  • Loading branch information
harazdovskiy authored Aug 16, 2022
2 parents b1d9c35 + ab44d74 commit cd731fd
Show file tree
Hide file tree
Showing 21 changed files with 178 additions and 101 deletions.
4 changes: 3 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ jobs:
steps:
- checkout
- install_deps
- run: yarn test
- run: yarn build
- run: yarn lint:ci
- run: yarn type-check
- run: yarn test
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ lib/
node_modules/
coverage/
renovate.json
tsconfig.json
6 changes: 4 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"root": true,
"extends": ["@shelf/eslint-config/backend"],
"extends": [
"@shelf/eslint-config/typescript"
],
"rules": {
"no-console": 0
"@typescript-eslint/no-var-requires": "off"
}
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.idea/
coverage/
node_modules/
dist/
lib/
yarn.lock
*.log
.DS_Store
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package.json
7 changes: 2 additions & 5 deletions jest-preset.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
const {resolve} = require('path');
const preset = require('./lib');

module.exports = {
globalSetup: resolve(__dirname, './setup.js'),
globalTeardown: resolve(__dirname, './teardown.js')
};
module.exports = preset;
9 changes: 9 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) Gemshelf Inc. (shelf.io)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 changes: 25 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shelf/jest-elasticsearch",
"version": "4.0.0",
"version": "4.1.0",
"description": "Run your tests using jest & elasticsearch-local",
"keywords": [
"jest",
Expand All @@ -20,25 +20,30 @@
"files": [
"jest-es-config.js",
"jest-preset.js",
"setup.js",
"teardown.js"
"lib/"
],
"scripts": {
"build": "rm -rf lib/ && yarn build:types && babel src --out-dir lib --ignore '**/*.test.ts' --extensions '.ts'",
"build:types": "tsc --emitDeclarationOnly --declaration --isolatedModules false --declarationDir lib",
"coverage": "jest --coverage",
"lint": "eslint . --ext .js,.json --fix",
"lint:ci": "eslint . --ext .js,.json --quiet",
"test": "jest src"
"lint": "eslint . --ext .js,.ts,.json --fix",
"lint:ci": "eslint . --ext .js,.ts,.json",
"prepack": "yarn build",
"test": "export ENVIRONMENT=local && jest tests",
"type-check": "tsc --noEmit",
"type-check:watch": "npm run type-check -- --watch"
},
"lint-staged": {
"*.js": [
"eslint --fix",
"git add"
"*.{ts,js,}": [
"eslint --fix"
],
"*.{html,json,md,yml}": [
"prettier --write",
"git add"
"prettier --write"
]
},
"babel": {
"extends": "@shelf/babel-config/backend"
},
"prettier": "@shelf/prettier-config",
"jest": {
"preset": "./jest-preset.js"
Expand All @@ -48,14 +53,22 @@
"cwd": "0.10.0"
},
"devDependencies": {
"@babel/cli": "7.18.9",
"@babel/core": "7.18.9",
"@elastic/elasticsearch": "7.15.0",
"@shelf/babel-config": "1.2.0",
"@shelf/eslint-config": "0.19.0",
"@shelf/prettier-config": "0.0.7",
"@shelf/tsconfig": "0.0.8",
"@types/cwd": "^0.10.0",
"@types/jest": "28.1.3",
"@types/node": "16",
"eslint": "7.26.0",
"husky": "7.0.4",
"jest": "28.1.3",
"lint-staged": "12.1.2",
"prettier": "2.3.0"
"prettier": "2.3.0",
"typescript": "4.7.4"
},
"engines": {
"node": ">=16"
Expand Down
12 changes: 11 additions & 1 deletion renovate.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
{
"extends": ["github>shelfio/renovate-config"]
"extends": [
"config:base"
],
"labels": [
"dependencies",
"backend"
],
"ignoreDeps": [
"cimg/node",
"@types/node"
]
}
29 changes: 0 additions & 29 deletions src/elasticsearch.js

This file was deleted.

6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {resolve} from 'path';

module.exports = {
globalSetup: resolve(__dirname, './setup.js'),
globalTeardown: resolve(__dirname, './teardown.js')
};
39 changes: 0 additions & 39 deletions src/insert-documents.js

This file was deleted.

5 changes: 3 additions & 2 deletions setup.js → src/setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {resolve} = require('path');
import {resolve} from 'path';
import {start} from '@shelf/elasticsearch-local';

const cwd = require('cwd');
const {start} = require('@shelf/elasticsearch-local');

module.exports = async function startES() {
const config = require(resolve(cwd(), 'jest-es-config.js'))();
Expand Down
2 changes: 1 addition & 1 deletion teardown.js → src/teardown.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {stop} = require('@shelf/elasticsearch-local');
import {stop} from '@shelf/elasticsearch-local';

module.exports = async function stopES() {
stop();
Expand Down
2 changes: 1 addition & 1 deletion src/elasticsearch.test.js → tests/elasticsearch.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {getClient} = require('./elasticsearch');
import {getClient} from './elasticsearch';

it('should export getClient function', function () {
expect(getClient).toBeInstanceOf(Function);
Expand Down
34 changes: 34 additions & 0 deletions tests/elasticsearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {Client} from '@elastic/elasticsearch';
import {
ApiResponse,
TransportRequestOptions,
TransportRequestPromise
} from '@elastic/elasticsearch/lib/Transport';

let client: undefined | Client;

export function search(options: TransportRequestOptions): TransportRequestPromise<ApiResponse> {
const es = getClient();

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return es.search(options);
}

export async function refreshAllIndexes(): Promise<TransportRequestPromise<ApiResponse>> {
const es = getClient();

return es.indices.refresh({index: '_all'});
}

export function getClient(): Client {
if (!client) {
if (!process.env.ES_URL) {
throw new Error('Please provide ES_URL env var first');
}

client = new Client({node: process.env.ES_URL, requestTimeout: 14 * 1000});
}

return client;
}
48 changes: 48 additions & 0 deletions tests/insert-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {getClient, refreshAllIndexes} from './elasticsearch';

type Document = {id: string};

type Target = (
| {update: {_index: string; _id: string; routing: string}}
| {doc: Document; doc_as_upsert: boolean}
)[];

export default async function insertDocuments(documents: Document[]): Promise<void> {
const body = generateTargetBody(documents);
await bulk(body, {index: 'documents'});
await refreshAllIndexes();
}

function generateTargetBody(documents: Document[]): Target {
const targets = [] as Target;
documents.map(document => {
const body = [
{update: {_index: 'documents', _id: document.id, routing: document.id}},
{
doc: document,
doc_as_upsert: true
}
];

return targets.push(...body);
});

return targets;
}

async function bulk(body: Target, {index}: {index?: string} = {}) {
const client = getClient();
const params: {body: Target; index?: string} = {
body
};

if (index) {
params.index = index;
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const {body: response} = await client.bulk(params);

return response;
}
2 changes: 1 addition & 1 deletion src/documents.mock.js → tests/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = [
export default [
{
id: 'some-doc-id-1',
name: 'some-name-1'
Expand Down
6 changes: 3 additions & 3 deletions src/search-by-term.test.js → tests/search-by-term.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const documents = require('./documents.mock');
const getDocuments = require('./search-by-term');
const insertDocuments = require('./insert-documents');
import documents from './mocks';
import getDocuments from './search-by-term';
import insertDocuments from './insert-helper';

describe('getDocuments', () => {
beforeAll(async () => {
Expand Down
19 changes: 16 additions & 3 deletions src/search-by-term.js → tests/search-by-term.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
const {search} = require('./elasticsearch');
import {search} from './elasticsearch';

module.exports = async function getDocuments({index, id, startFrom = 0}) {
type GetDocumentsResponse = {
items: Array<{[key: string]: string}>;
totalCount: number;
};

export default async function getDocuments({
index,
id,
startFrom = 0
}: {
index: string;
id: string;
startFrom: number;
}): Promise<GetDocumentsResponse> {
const body = {
_source: {
includes: ['_id', 'name', 'id']
Expand All @@ -21,4 +34,4 @@ module.exports = async function getDocuments({index, id, startFrom = 0}) {
} = await search({index, body});

return {items: hits, totalCount: total};
};
}
8 changes: 8 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "@shelf/tsconfig/backend",
"compilerOptions": {
"strict": true
},
"exclude": ["node_modules"],
"include": ["src"]
}

0 comments on commit cd731fd

Please sign in to comment.