Skip to content
This repository was archived by the owner on May 8, 2025. It is now read-only.

Commit 4391d2c

Browse files
committed
closes vadimdemedes#39, closes vadimdemedes#41; 2022 renovation (v3.0.1)
- es6 rewrite - use of urlsearchparams instead of querystring (querystring is deprecated in favor of urlsearchparams) - use of axios instead of got (got <11.8.5 allows a redirect to a UNIX socket)
1 parent 93b76e5 commit 4391d2c

File tree

10 files changed

+3741
-147
lines changed

10 files changed

+3741
-147
lines changed

.editorconfig

Lines changed: 0 additions & 12 deletions
This file was deleted.

.github/workflows/deploy.yml

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
name: npm-publish
2+
on:
3+
push:
4+
branches:
5+
- master
6+
jobs:
7+
npm-publish:
8+
name: npm-publish
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
# Publish to Node Package Manager
13+
- name: Checkout Repo
14+
uses: actions/checkout@main
15+
16+
- name: Setup Node.js (NPM)
17+
uses: actions/setup-node@master
18+
with:
19+
node-version: '16.x'
20+
registry-url: 'https://registry.npmjs.org'
21+
22+
- name: Use cached node_modules
23+
uses: actions/cache@master
24+
with:
25+
path: node_modules
26+
key: nodeModules-${{ hashFiles('**/yarn.lock') }}
27+
restore-keys: |
28+
nodeModules-
29+
30+
- name: Install dependencies
31+
run: yarn install --frozen-lockfile
32+
env:
33+
CI: true
34+
35+
- name: Update Publish Config
36+
run: sed -i 's^registry-url^registry.npmjs.org^' package.json
37+
38+
- name: Publish to NPM
39+
run: npm publish --access public
40+
env:
41+
CI: true
42+
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
43+
44+
gpr-publish:
45+
name: gpr-publish
46+
runs-on: ubuntu-latest
47+
48+
steps:
49+
# Publish to GitHub Package Registry
50+
- name: Checkout Repo
51+
uses: actions/checkout@main
52+
53+
- name: Store lowercase actor name
54+
run: |
55+
echo 'actor_name<<EOF' >> $GITHUB_ENV
56+
echo ${{ github.actor }} | tr "A-Z" "a-z" >> $GITHUB_ENV
57+
echo 'EOF' >> $GITHUB_ENV
58+
59+
- name: Store package name
60+
run: |
61+
echo 'package_name<<EOF' >> $GITHUB_ENV
62+
grep -Po '"name": *\K"[^"]*"' package.json | grep -oP '"\K[^"\047]+(?=["\047])' >> $GITHUB_ENV
63+
echo 'EOF' >> $GITHUB_ENV
64+
65+
- name: Setup Node.js (GPR)
66+
uses: actions/setup-node@master
67+
with:
68+
node-version: '12.x'
69+
registry-url: https://npm.pkg.github.com/
70+
scope: '${{ env.actor_name }}'
71+
72+
- name: Use cached node_modules
73+
uses: actions/cache@master
74+
with:
75+
path: node_modules
76+
key: nodeModules-${{ hashFiles('**/yarn.lock') }}
77+
restore-keys: |
78+
nodeModules-
79+
80+
- name: Install dependencies
81+
run: yarn install --frozen-lockfile
82+
env:
83+
CI: true
84+
85+
- name: Update Package Name
86+
run: |
87+
sed -i 's,"name": "${{ env.package_name }}","name": "@${{ env.actor_name }}/${{ env.package_name }}",' package.json
88+
cat package.json
89+
90+
- name: Update Publish Config
91+
run: |
92+
sed -i 's^registry-url^npm.pkg.github.com/@${{ env.actor_name }}^' package.json
93+
cat package.json
94+
95+
- name: Publish to GitHub Package Registry
96+
run: npm publish --access public
97+
env:
98+
CI: true
99+
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
*Zone.Identifier
12
.DS_Store
23
node_modules
4+
.cache
5+
.vscode

.travis.yml

Lines changed: 0 additions & 4 deletions
This file was deleted.

index.js

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
'use strict';
1+
import axios from 'axios';
22

3-
const qs = require('querystring');
4-
const got = require('got');
5-
6-
class Client {
3+
export default class Client {
74
constructor(id, apiKey) {
85
if (!id) {
96
throw new TypeError('Expected a Custom Search Engine ID');
@@ -18,31 +15,15 @@ class Client {
1815
this.id = id;
1916
}
2017

21-
search(query, options) {
18+
async search(query, options) {
2219
if (!query) {
2320
throw new TypeError('Expected a query');
2421
}
2522

2623
const url = `${this.endpoint}/customsearch/v1?${this.buildQuery(query, options)}`;
2724

28-
return got(url, {json: true}).then(res => {
29-
const items = res.body.items || [];
30-
31-
return items.map(item => ({
32-
type: item.mime,
33-
width: item.image.width,
34-
height: item.image.height,
35-
size: item.image.byteSize,
36-
url: item.link,
37-
thumbnail: {
38-
url: item.image.thumbnailLink,
39-
width: item.image.thumbnailWidth,
40-
height: item.image.thumbnailHeight
41-
},
42-
description: item.snippet,
43-
parentPage: item.image.contextLink
44-
}));
45-
});
25+
const result = await axios.get(url);
26+
return result.data.items;
4627
}
4728

4829
buildQuery(query, options) {
@@ -52,7 +33,7 @@ class Client {
5233
q: query.replace(/\s/g, '+'),
5334
searchType: 'image',
5435
cx: this.id,
55-
key: this.apiKey
36+
key: this.apiKey,
5637
};
5738

5839
if (options.page) {
@@ -79,8 +60,6 @@ class Client {
7960
result.safe = options.safe;
8061
}
8162

82-
return qs.stringify(result);
63+
return new URLSearchParams(result).toString();
8364
}
8465
}
85-
86-
module.exports = Client;

media/screenshot.png

-12 KB
Binary file not shown.

0 commit comments

Comments
 (0)