Skip to content

Commit

Permalink
feat(hooks): document secured api keys (#394)
Browse files Browse the repository at this point in the history
* feat(hooks): document secured api keys

copied from the react-instantsearch example

* fix boog
  • Loading branch information
Haroenv authored Jun 23, 2022
1 parent 4af14ef commit 9c43daa
Show file tree
Hide file tree
Showing 18 changed files with 9,322 additions and 30 deletions.
6 changes: 5 additions & 1 deletion InstantSearch.js/secured-api-keys/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ app.get('/', async (_, res) => {
res.send(indexWithServerData);
});

app.listen(8080);
const PORT = 8080;

app.listen(PORT, () =>
console.log(`Example app listening at http://localhost:${PORT}`)
);
9 changes: 9 additions & 0 deletions React InstantSearch Hooks/secured-api-keys/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
21 changes: 21 additions & 0 deletions React InstantSearch Hooks/secured-api-keys/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
/node_modules

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
5 changes: 5 additions & 0 deletions React InstantSearch Hooks/secured-api-keys/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"proseWrap": "never",
"trailingComma": "es5"
}
19 changes: 19 additions & 0 deletions React InstantSearch Hooks/secured-api-keys/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# secured-api-keys

_This project was generated with [create-instantsearch-app](https://github.com/algolia/create-instantsearch-app) by [Algolia](https://algolia.com)._

## Get started

To run this project locally, install the dependencies and run the local server:

```sh
npm install
npm run server
```

Alternatively, you may use [Yarn](https://http://yarnpkg.com/):

```sh
yarn
yarn server
```
32 changes: 32 additions & 0 deletions React InstantSearch Hooks/secured-api-keys/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "secured-api-keys",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"server": "npm run build && node server.js",
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix"
},
"dependencies": {
"algoliasearch": "4.13.1",
"express": "4.18.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-instantsearch-hooks-web": "6.29.0",
"react-scripts": "5.0.1"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions React InstantSearch Hooks/secured-api-keys/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">

<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.png">

<!--
Do not use @7 in production, use a complete version like x.x.x, see website for latest version:
https://community.algolia.com/react-instantsearch/Getting_started.html#load-the-algolia-theme
-->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/instantsearch.css@7/themes/algolia-min.css">

<title>secured-api-keys</title>
</head>

<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>

<div id="root"></div>

<script>
window.SERVER_DATA = __SERVER_DATA__;
</script>
</body>

</html>
15 changes: 15 additions & 0 deletions React InstantSearch Hooks/secured-api-keys/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"short_name": "secured-api-keys",
"name": "secured-api-keys Sample",
"icons": [
{
"src": "favicon.png",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
}
],
"start_url": "./index.html",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"template": "node",
"container": { "port": 8080, "startScript": "server" }
}
46 changes: 46 additions & 0 deletions React InstantSearch Hooks/secured-api-keys/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-disable import/no-commonjs */

const path = require('path');
const fs = require('fs');
const util = require('util');
const express = require('express');
const algoliasearch = require('algoliasearch');

const app = express();
const readFileAsync = util.promisify(fs.readFile);

const client = algoliasearch('B1G2GM9NG0', 'aadef574be1f9252bb48d4ea09b5cfe5');
const securedApiKey = client.generateSecuredApiKey(
'aadef574be1f9252bb48d4ea09b5cfe5',
{
restrictIndices: 'demo_ecommerce',
}
);

app.use(
express.static(path.join(__dirname, 'build'), {
index: false,
})
);

app.get('/', async (_, res) => {
const index = await readFileAsync(
path.join(__dirname, 'build', 'index.html'),
'utf-8'
);

const indexWithServerData = index.replace(
'__SERVER_DATA__',
JSON.stringify({
ALGOLIA_API_KEY: securedApiKey,
})
);

res.send(indexWithServerData);
});

const PORT = 8080;

app.listen(PORT, () =>
console.log(`Example app listening at http://localhost:${PORT}`)
);
46 changes: 46 additions & 0 deletions React InstantSearch Hooks/secured-api-keys/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
body {
font-family: sans-serif;
padding: 1em;
}

.ais-SearchBox {
margin: 1em 0;
}

.ais-Pagination {
margin-top: 1em;
}

.left-panel {
float: left;
width: 250px;
}

.right-panel {
margin-left: 260px;
}

.ais-InstantSearch {
max-width: 960px;
overflow: hidden;
margin: 0 auto;
}

.ais-Hits-item {
margin-bottom: 1em;
width: calc(50% - 1rem);
}

.ais-Hits-item img {
margin-right: 1em;
}

.hit-name {
margin-bottom: 0.5em;
}

.hit-description {
color: #888;
font-size: 14px;
margin-bottom: 0.5em;
}
50 changes: 50 additions & 0 deletions React InstantSearch Hooks/secured-api-keys/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React from 'react';
import {
InstantSearch,
Hits,
SearchBox,
Pagination,
Highlight,
ClearRefinements,
RefinementList,
Configure,
} from 'react-instantsearch-hooks-web';
import './App.css';

function App({ searchClient }) {
return (
<div className="ais-InstantSearch">
<h1>React InstantSearch e-commerce demo</h1>
<InstantSearch indexName="demo_ecommerce" searchClient={searchClient}>
<div className="left-panel">
<ClearRefinements />
<h2>Brands</h2>
<RefinementList attribute="brand" />
<Configure hitsPerPage={8} />
</div>
<div className="right-panel">
<SearchBox />
<Hits hitComponent={Hit} />
<Pagination />
</div>
</InstantSearch>
</div>
);
}

function Hit(props) {
return (
<div>
<img src={props.hit.image} align="left" alt={props.hit.name} />
<div className="hit-name">
<Highlight attribute="name" hit={props.hit} />
</div>
<div className="hit-description">
<Highlight attribute="description" hit={props.hit} />
</div>
<div className="hit-price">${props.hit.price}</div>
</div>
);
}

export default App;
10 changes: 10 additions & 0 deletions React InstantSearch Hooks/secured-api-keys/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
body,
h1 {
margin: 0;
padding: 0;
}

body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica,
Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';
}
16 changes: 16 additions & 0 deletions React InstantSearch Hooks/secured-api-keys/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import ReactDOM from 'react-dom';
import algoliasearch from 'algoliasearch/lite';
import App from './App';
import './index.css';

const SERVER_DATA = window.SERVER_DATA;

delete window.SERVER_DATA;

const searchClient = algoliasearch('B1G2GM9NG0', SERVER_DATA.ALGOLIA_API_KEY);

ReactDOM.render(
<App searchClient={searchClient} />,
document.getElementById('root')
);
Loading

0 comments on commit 9c43daa

Please sign in to comment.