Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue when returning authors phrases by search #18

Merged
merged 3 commits into from
Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 134 additions & 6 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@types/iconv-lite": "^0.0.1",
"@types/jest": "^28.1.6",
"@types/node": "18.6.1",
"@types/qs": "^6.9.7",
"@typescript-eslint/eslint-plugin": "^5.30.7",
"@typescript-eslint/parser": "^5.30.7",
"eslint": "^8.20.0",
Expand All @@ -54,6 +55,7 @@
"dependencies": {
"cheerio": "^1.0.0-rc.12",
"iconv-lite": "^0.6.3",
"qs": "^6.11.0",
"slugify": "^1.6.5",
"undici": "^5.8.0"
}
Expand Down
32 changes: 29 additions & 3 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
import pensador from '../index';

it('Works', async () => {
const result = await pensador({ term: 'Elon Musk', max: 5 });
expect(result.searchTerm).toBe('frases_de_elon_musk');
describe('Pensador', () => {
it('Works', async () => {
const result = await pensador({ term: 'Elon Musk', max: 5 });
expect(result.searchTerm).toBe('elon_musk');
});
it('Should return phrases', async () => {
const result = await pensador({ term: 'Elon Musk', max: 5 });
expect(result.total).toBeGreaterThan(0);
});
it('Should return an error when the term is not provided', async () => {
try {
await pensador({
max: 5,
term: '',
});
} catch (err: any) {
expect(err.message).toBe('The term is required');
}
});
it('Should return an error when the max is not provided', async () => {
try {
await pensador({
max: 0,
term: 'Elon Musk',
});
} catch (err: any) {
expect(err.message).toBe('The max is required');
}
});
});
2 changes: 1 addition & 1 deletion src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const BASE_URL = 'https://www.pensador.com/';
const BASE_URL = 'https://www.pensador.com';

export { BASE_URL };
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default async (options: IOptions) => {
_throw('A search term must be defined');
}

const searchTerm = slugify(`frases de ${options.term}`, {
const searchTerm = slugify(`${options.term}`, {
replacement: '_',
remove: /[*+~.()'"!:@]/g,
lower: true,
Expand Down
28 changes: 21 additions & 7 deletions src/services/fetchPage.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import iconv from 'iconv-lite';
import { request } from 'undici';
import qs from 'qs';
import { isSuccessfulRequest } from '../utils/isSuccessfulRequest';

async function fetchPage(searchTerm: string, current = 1, baseUrl: string) {
return new Promise((resolve, reject) => {
return request(`${baseUrl}/${searchTerm}/${current}`)
.then((res) => res.body.arrayBuffer())
.then((arrayBuffer) => iconv.decode(Buffer.from(arrayBuffer), 'utf-8').toString())
.then((body) => resolve(body))
.catch((err) => reject(err));
export async function fetchPage(searchTerm: string, current = 1, baseUrl: string) {
const params = qs.stringify({
q: searchTerm,
p: current,
});
try {
const res = await request(`${baseUrl}/busca.php?${params}`);
if (!isSuccessfulRequest(res.statusCode)) {
throw new Error(`Error fetching ${searchTerm}, status code: ${res.statusCode}`);
}
const arrayBuffer = await res.body.arrayBuffer();
const body = iconv.decode(Buffer.from(arrayBuffer), 'utf-8').toString();
return body;
} catch (error) {
if (error instanceof Error) {
throw error;
}
throw new Error(`Error fetching ${searchTerm}`);
}
}

export default fetchPage;
10 changes: 8 additions & 2 deletions src/utils/_throw.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export default function _throw(m: string | string[]) {
throw m;
export default function _throw(message: string | string[] | Error) {
if (Array.isArray(message)) {
throw new Error(message.join('\n'));
}
if (message instanceof Error) {
throw message.message;
}
throw message;
}
Loading