Skip to content

Commit

Permalink
feat(typescript): added typescript support (#17)
Browse files Browse the repository at this point in the history
* feat(typescript): added autocomplete types

* feat(typescript): added retrieve, enrichment, cleaner and identity types

* feat(typescript): finished all endpoints and added types to errors

* feat(typescript): added types to examples

* feat(typescript): fixes based on questions

* feat(typescript): added pretty param to all endpoints

* feat(typescript): added rate limiting data and types

* feat(typescript): fixed bulk test

* feat(typescript): bumped to version 2.0.0

* fix readme

* fixes

Co-authored-by: Diego Abizaid <diegoabizaid@gmail.com>
Co-authored-by: vvillait88 <vvillait88@yahoo.com>
  • Loading branch information
3 people authored Apr 19, 2022
1 parent 1091861 commit 1ae87d5
Show file tree
Hide file tree
Showing 37 changed files with 4,613 additions and 3,925 deletions.
21 changes: 21 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: [
'airbnb-base',
'airbnb-typescript/base',
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
project: './tsconfig.json',
},
plugins: [
'@typescript-eslint',
],
rules: {
},
};
61 changes: 0 additions & 61 deletions .eslintrc.js

This file was deleted.

34 changes: 29 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This library bundles up PDL API requests into simple function calls, making it e
- Tiny <2KB size gzip
- Works in Node.js and in Browser
- Supports all People Data Labs API endpoints
- Built-in Typescript support

## Table of Contents
- [🔧 Installation](#installation)
Expand Down Expand Up @@ -64,11 +65,33 @@ PDLJSClient.person.enrichment({ phone: '4155688415' }).then((data) => {
console.log(error);
});

// By Bulk Enrichment
const records = {
requests: [
{
params: {
profile: ['linkedin.com/in/seanthorne'],
},
},
{
params: {
profile: ['linkedin.com/in/randrewn'],
},
},
],
};

PDLJSClient.person.bulk(records).then((data) => {
console.log(data.items);
}).catch((error) => {
console.log(error);
});

// By Search (SQL)
const sqlQuery = "SELECT * FROM person WHERE location_country='mexico' AND job_title_role='health'AND phone_numbers IS NOT NULL;"

PDLJSClient.person.search.sql({ searchQuery: sqlQuery, size: 10 }).then((data) => {
console.log(data['total']);
console.log(data.total);
}).catch((error) => {
console.log(error);
});
Expand All @@ -87,7 +110,7 @@ const esQuery = {
}

PDLJSClient.person.search.elastic({ searchQuery: esQuery, size: 10 }).then((data) => {
console.log(data['total']);
console.log(data.total);
}).catch((error) => {
console.log(error);
});
Expand Down Expand Up @@ -120,7 +143,7 @@ PDLJSClient.company.enrichment({ website: 'peopledatalabs.com' }).then((data) =>
const sqlQuery = "SELECT * FROM company WHERE tags='big data' AND industry='financial services' AND location.country='united states';"

PDLJSClient.company.search.sql({ searchQuery: sqlQuery, size: 10 }).then((data) => {
console.log(data['total']);
console.log(data.total);
}).catch((error) => {
console.log(error);
});
Expand All @@ -139,7 +162,7 @@ const esQuery = {
}

PDLJSClient.company.search.elastic({ searchQuery: esQuery, size: 10 }).then((data) => {
console.log(data['total']);
console.log(data.total);
}).catch((error) => {
console.log(error);
});
Expand Down Expand Up @@ -184,6 +207,7 @@ PDLJSClient.school.cleaner({ name: 'university of oregon' }).then((data) => {
| API Endpoint | PDLJS Function |
|-|-|
| [Person Enrichment API](https://docs.peopledatalabs.com/docs/enrichment-api) | `PDLJS.person.enrichment(...params)` |
| [Person Bulk Person Enrichment API](https://docs.peopledatalabs.com/docs/bulk-enrichment-api) | `PDLJS.person.bulk(...records)` |
| [Person Search API](https://docs.peopledatalabs.com/docs/search-api) | SQL: `PDLJS.person.search.sql(...params)` <br/> Elasticsearch: `PDLJS.person.search.elastic(...params)`|
| [Person Retrieve API](https://docs.peopledatalabs.com/docs/person-retrieve-api) | `PDLJS.person.retrieve(...params)` |
| [Person Identify API](https://docs.peopledatalabs.com/docs/identify-api) | `PDLJS.person.identify(...params)` |
Expand Down Expand Up @@ -242,7 +266,7 @@ You can pass your query to these methods using the special `searchQuery` functio
const sqlQuery = "SELECT * FROM company WHERE website='peopledatalabs.com';"

PDLJSClient.company.search.sql({ searchQuery: sqlQuery, size: 10 }).then((data) => {
console.log(data['total']);
console.log(data.total);
}).catch((error) => {
console.log(error);
});
Expand Down
3 changes: 0 additions & 3 deletions example/.eslintrc.js

This file was deleted.

2 changes: 1 addition & 1 deletion example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const records = {
};

PDLJSClient.person.bulk(records).then((data) => {
console.log(data);
console.log(data.items);
}).catch((error) => {
console.log(error);
});
Expand Down
18 changes: 12 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "peopledatalabs",
"version": "1.1.14",
"version": "2.0.0",
"description": "An universal client for the People Data Labs API",
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.m.js",
"umd:main": "dist/index.umd.js",
"source": "src/index.js",
"types": "dist/index.d.ts",
"source": "src/index.ts",
"scripts": {
"build": "rm -rf dist && microbundle",
"dev": "microbundle watch",
Expand Down Expand Up @@ -36,20 +37,25 @@
"bugs": {
"url": "https://github.com/peopledatalabs/peopledatalabs-js/issues"
},
"homepage": "https://github.com/peopledatalabs/peopledatalabs-js#readme",
"homepage": "https://www.npmjs.com/package/peopledatalabs",
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.16.0",
"@typescript-eslint/parser": "^5.16.0",
"chai": "^4.3.6",
"dotenv": "^16.0.0",
"eslint": "^6.8.0",
"eslint": "^8.11.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-plugin-import": "^2.24.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-airbnb-typescript": "^16.1.4",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.24.0",
"eslint-plugin-react-hooks": "^1.7.0",
"eslint-plugin-unused-imports": "^2.0.0",
"esm": "^3.2.25",
"microbundle": "^0.14.2",
"mocha": "^9.2.2"
"mocha": "^9.2.2",
"typescript": "^4.6.3"
},
"dependencies": {
"axios": "^0.24.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { check, errorHandler } from '../../errors';

import axios from 'axios';
import { check, errorHandler } from '../../errors';
import type { AutoCompleteParams, AutoCompleteResponse } from '../../types/autocomplete-types';
import { parseRateLimitingResponse } from '../../utils/api-utils';

export default (basePath, apiKey, params) => new Promise((resolve, reject) => {
export default (
basePath: string,
apiKey: string,
params: AutoCompleteParams,
) => new Promise<AutoCompleteResponse>((resolve, reject) => {
check(params, basePath, apiKey, null, 'autocomplete').then(() => {
const {
field, text, size, pretty,
Expand All @@ -19,16 +24,16 @@ export default (basePath, apiKey, params) => new Promise((resolve, reject) => {
'Accept-Encoding': 'gzip',
};

axios.get(`${basePath}/autocomplete`, {
axios.get<AutoCompleteResponse>(`${basePath}/autocomplete`, {
params: {
api_key: apiKey,
...autocompleteParams,
},
headers,
})
.then((data) => {
if (data?.data?.status === 200) {
resolve(data.data);
.then((response) => {
if (response?.data?.status === 200) {
resolve(parseRateLimitingResponse(response));
}
})
.catch((error) => {
Expand Down
27 changes: 0 additions & 27 deletions src/endpoints/bulk/index.js

This file was deleted.

26 changes: 26 additions & 0 deletions src/endpoints/bulk/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import axios from 'axios';
import { check, errorHandler } from '../../errors';
import { BulkPersonEnrichmentParams, BulkPersonEnrichmentResponse } from '../../types/bulk-types';
import { parseRateLimitingResponse } from '../../utils/api-utils';

export default (basePath: string, apiKey: string, records: BulkPersonEnrichmentParams) => {
const headers = {
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip',
'X-Api-Key': apiKey,
};

return new Promise<BulkPersonEnrichmentResponse>((resolve, reject) => {
check(records, basePath, apiKey, 'Records', 'bulk').then(() => {
axios.post<BulkPersonEnrichmentResponse>(`${basePath}/person/bulk`, records, { headers })
.then((response) => {
resolve(parseRateLimitingResponse(response));
})
.catch((error) => {
reject(errorHandler(error));
});
}).catch((error) => {
reject(error);
});
});
};
29 changes: 0 additions & 29 deletions src/endpoints/cleaner/index.js

This file was deleted.

36 changes: 36 additions & 0 deletions src/endpoints/cleaner/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import axios from 'axios';
import { check, errorHandler } from '../../errors';
import { BaseResponse } from '../../types/api-types';
import { CleanerType } from '../../types/cleaner-types';
import { parseRateLimitingResponse } from '../../utils/api-utils';

export default <T, K extends BaseResponse> (
basePath: string,
apiKey: string,
params: T,
type: CleanerType,
) => new Promise<K>((resolve, reject) => {
check(params, basePath, apiKey, null, 'cleaner').then(() => {
const headers = {
'Accept-Encoding': 'gzip',
};

axios.get<K>(`${basePath}/${type}/clean`, {
params: {
api_key: apiKey,
...params,
},
headers,
})
.then((response) => {
if (response?.data?.status === 200) {
resolve(parseRateLimitingResponse(response));
}
})
.catch((error) => {
reject(errorHandler(error));
});
}).catch((error) => {
reject(error.message);
});
});
Loading

0 comments on commit 1ae87d5

Please sign in to comment.