Skip to content

Commit

Permalink
closes vadimdemedes#39, closes vadimdemedes#41; 2022 renovation (v3.0.1)
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
obrobrio2000 committed Jul 18, 2022
1 parent 93b76e5 commit 4391d2c
Show file tree
Hide file tree
Showing 10 changed files with 3,741 additions and 147 deletions.
12 changes: 0 additions & 12 deletions .editorconfig

This file was deleted.

99 changes: 99 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
name: npm-publish
on:
push:
branches:
- master
jobs:
npm-publish:
name: npm-publish
runs-on: ubuntu-latest

steps:
# Publish to Node Package Manager
- name: Checkout Repo
uses: actions/checkout@main

- name: Setup Node.js (NPM)
uses: actions/setup-node@master
with:
node-version: '16.x'
registry-url: 'https://registry.npmjs.org'

- name: Use cached node_modules
uses: actions/cache@master
with:
path: node_modules
key: nodeModules-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
nodeModules-
- name: Install dependencies
run: yarn install --frozen-lockfile
env:
CI: true

- name: Update Publish Config
run: sed -i 's^registry-url^registry.npmjs.org^' package.json

- name: Publish to NPM
run: npm publish --access public
env:
CI: true
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}

gpr-publish:
name: gpr-publish
runs-on: ubuntu-latest

steps:
# Publish to GitHub Package Registry
- name: Checkout Repo
uses: actions/checkout@main

- name: Store lowercase actor name
run: |
echo 'actor_name<<EOF' >> $GITHUB_ENV
echo ${{ github.actor }} | tr "A-Z" "a-z" >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV
- name: Store package name
run: |
echo 'package_name<<EOF' >> $GITHUB_ENV
grep -Po '"name": *\K"[^"]*"' package.json | grep -oP '"\K[^"\047]+(?=["\047])' >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV
- name: Setup Node.js (GPR)
uses: actions/setup-node@master
with:
node-version: '12.x'
registry-url: https://npm.pkg.github.com/
scope: '${{ env.actor_name }}'

- name: Use cached node_modules
uses: actions/cache@master
with:
path: node_modules
key: nodeModules-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
nodeModules-
- name: Install dependencies
run: yarn install --frozen-lockfile
env:
CI: true

- name: Update Package Name
run: |
sed -i 's,"name": "${{ env.package_name }}","name": "@${{ env.actor_name }}/${{ env.package_name }}",' package.json
cat package.json
- name: Update Publish Config
run: |
sed -i 's^registry-url^npm.pkg.github.com/@${{ env.actor_name }}^' package.json
cat package.json
- name: Publish to GitHub Package Registry
run: npm publish --access public
env:
CI: true
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
*Zone.Identifier
.DS_Store
node_modules
.cache
.vscode
4 changes: 0 additions & 4 deletions .travis.yml

This file was deleted.

35 changes: 7 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
'use strict';
import axios from 'axios';

const qs = require('querystring');
const got = require('got');

class Client {
export default class Client {
constructor(id, apiKey) {
if (!id) {
throw new TypeError('Expected a Custom Search Engine ID');
Expand All @@ -18,31 +15,15 @@ class Client {
this.id = id;
}

search(query, options) {
async search(query, options) {
if (!query) {
throw new TypeError('Expected a query');
}

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

return got(url, {json: true}).then(res => {
const items = res.body.items || [];

return items.map(item => ({
type: item.mime,
width: item.image.width,
height: item.image.height,
size: item.image.byteSize,
url: item.link,
thumbnail: {
url: item.image.thumbnailLink,
width: item.image.thumbnailWidth,
height: item.image.thumbnailHeight
},
description: item.snippet,
parentPage: item.image.contextLink
}));
});
const result = await axios.get(url);
return result.data.items;
}

buildQuery(query, options) {
Expand All @@ -52,7 +33,7 @@ class Client {
q: query.replace(/\s/g, '+'),
searchType: 'image',
cx: this.id,
key: this.apiKey
key: this.apiKey,
};

if (options.page) {
Expand All @@ -79,8 +60,6 @@ class Client {
result.safe = options.safe;
}

return qs.stringify(result);
return new URLSearchParams(result).toString();
}
}

module.exports = Client;
Binary file removed media/screenshot.png
Binary file not shown.
Loading

0 comments on commit 4391d2c

Please sign in to comment.