Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
@rtd/use-suggestions
Advanced tools
A React hook for fetching search suggestions securely through a Next.js proxy. This package includes a custom hook, helper functions for creating proxy routes, types, and constants to simplify integration with your React/TypeScript projects.
A React hook for fetching search suggestions securely through a Next.js proxy. This package includes a custom hook, helper functions for creating proxy routes, types, and constants to simplify integration with your React/TypeScript projects.
To install the package locally, you can use yarn add
or npm install
with a local file path:
yarn add @rtd/use-suggestions
or
npm install @rtd/use-suggestions
To protect your API key, you must create a Next.js API route that acts as a proxy to the external API. Use the provided createSuggestionsProxyHandler helper function for this.
For Next.js 12 (pages/api/suggestions.ts):
import { createSuggestionsProxyHandler } from '@rtd/use-suggestions'
export default createSuggestionsProxyHandler({
apiKey: process.env.API_KEY || '',
targetUrl: 'https://api.suggestions.com/endpoint',
})
For Next.js 13 (app/api/suggestions/route.ts):
import { createSuggestionsProxyHandler } from '@rtd/use-suggestions'
import { NextRequest, NextResponse } from 'next/server'
const handler = createSuggestionsProxyHandler({
apiKey: process.env.API_KEY || '',
targetUrl: 'https://api.suggestions.com/endpoint',
})
export const GET = (req: NextRequest) => handler(req, new NextResponse())
export const POST = (req: NextRequest) => handler(req, new NextResponse())
Import the useSuggestions
hook, types, and constants in your React component:
import React, { useState } from 'react'
import useSuggestions, { SuggestionsOptions } from '@rtd/use-suggestions'
Here’s an example of how to use the useSuggestions
hook in a React component:
const SuggestionComponent: React.FC = () => {
const [query, setQuery] = useState('')
const options: SuggestionsOptions = {
includeStops: true,
includeConcerts: true,
location: { lat: 39.7392, lng: -104.9903 },
debounceTime: 400,
}
const { suggestions, isLoading, error } = useSuggestions(query, options)
return (
<div>
<input
type="text"
value={query}
onChange={(e) => setQuery(e.target.value)}
placeholder="Search for suggestions"
/>
{isLoading && <p>Loading...</p>}
{error && <p>Error: {error.message}</p>}
<ul>
{suggestions.map((suggestion) => (
<li key={suggestion.slug}>{suggestion.name}</li>
))}
</ul>
</div>
)
}
export default SuggestionComponent
useSuggestions
Fetches search suggestions from the API.
Parameters:
rawQuery: string
- The search query string.options: SuggestionsOptions
- Configuration options for the suggestions.proxyPath?: string
- (Optional) Relative path to the proxy route. Defaults to /api/suggestions.Returns:
suggestions: SearchableSuggestion[]
- An array of suggestions.isLoading: boolean
- Loading state.error: Error | null
- Error state.The package includes several types to help with type safety in your project:
SuggestionsOptions
Configuration options for the suggestions.
interface SuggestionsOptions {
includeStops?: boolean
includeRoutes?: boolean
includeLocations?: boolean
includeDestinations?: boolean
includeConcerts?: boolean
location?: {
lat: number
lng: number
}
debounceTime?: number
}
SearchableSuggestion
Base interface for a searchable suggestion.
/**
* Suggestion Types Returned by the Server
*/
export type SuggestionType =
| 'location'
| 'concert'
| 'stop'
| 'station'
| 'route'
| 'destination'
/**
* Point Coordinates
*/
export interface Point {
lat: number
lng: number
}
/**
* Base Suggestion with Common Fields
*/
export interface BaseSuggestion extends Point {
suggestionType: SuggestionType
name: string
label: string
slug: string
score: number
}
The package includes the following constants:
DEBOUNCE_TIME_IN_MS
Default debounce time for the search query.
export const DEBOUNCE_TIME_IN_MS = 432
If you attempt to use the hook without first creating a proxy route, you will see the following error:
Proxy route not found: "/api/suggestions".
Ensure the proxy route exists and is set up using 'createSuggestionsProxyHandler'.
Solution: Follow the Setup section to create the API route.
If a developer attempts to pass an external API URL instead of a relative path to the hook:
useSuggestions('query', options, 'https://api.suggestions.com');
The hook throws a validation error:
Invalid proxyPath: "https://api.suggestions.com".
The proxyPath must be a relative path starting with "/".
Solution: Use a relative path, such as /api/suggestions.
To build the package, run:
yarn build
This will compile the TypeScript files and output the results in the dist
directory.
This project is licensed under the MIT License.
FAQs
A React hook for fetching search suggestions securely through a Next.js proxy. This package includes a custom hook, helper functions for creating proxy routes, types, and constants to simplify integration with your React/TypeScript projects.
The npm package @rtd/use-suggestions receives a total of 23 weekly downloads. As such, @rtd/use-suggestions popularity was classified as not popular.
We found that @rtd/use-suggestions demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.