-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [SC-25948] 🔧 Update code organization in NameGraph SDK (#486)
* feat: [SC-25948] 🔧 Update code organization in NameGraph SDK * feat: [SC-25948] 📝 Add README to NameGraph SDK * feat: [SC-25948] ♻️ Syntax updates in NameGraph SDK * feat: [SC-25948] ♻️ Reduce changesets files from 2 to 1 * feat: [SC-25975] 📝 Update namegraph.dev README * feat: [SC-25975] 📦 Update NameGuard SDK package and app that uses it * feat: [SC-25948] ♻️ Update name for NameGraph instance creation inside package README * feat: [SC-25948] ♻️ Remove duplicated changeset file * feat: [SC-25948] ♻️ Update README of NameGraph SDK * feat: [SC-25948] ♻️ Refactor Docs URL of NameGraph.dev * feat: [SC-25948] 🎨 Setup beta badge in NameGraph Dev app * hotfix: [SC-25948] 🚑 Builds hotfix * hotfix: [SC-25948] 🚑 Update pnpm lock * hotfix: [SC-25948] 🚑 Update pnpm lock file * hotfix: [SC-25948] 🚑 Update NameGraph endpoint URL in SDK README * Create page.tsx * Revert "Create page.tsx" This reverts commit e70cad2.
- Loading branch information
1 parent
b5dd3d1
commit 87793a5
Showing
19 changed files
with
559 additions
and
395 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@namehash/namegraph-sdk": minor | ||
--- | ||
|
||
Add README to NameGraph SDK and update package's code organization |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,3 @@ | ||
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). | ||
# NameGraph SDK use cases | ||
|
||
## Getting Started | ||
|
||
First, run the development server: | ||
|
||
```bash | ||
npm run dev | ||
# or | ||
yarn dev | ||
# or | ||
pnpm dev | ||
# or | ||
bun dev | ||
``` | ||
|
||
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. | ||
|
||
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file. | ||
|
||
This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font. | ||
|
||
## Learn More | ||
|
||
To learn more about Next.js, take a look at the following resources: | ||
|
||
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. | ||
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. | ||
|
||
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! | ||
|
||
## Deploy on Vercel | ||
|
||
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. | ||
|
||
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. | ||
This is a [Next.js](https://nextjs.org/) application that showcases different [`NameGraph SDK`](https://github.com/namehash/namekit/tree/main/packages/namegraph-sdk) use cases. |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,6 @@ | ||
import { writersBlockSuggestions } from "./writers-block-suggestions"; | ||
import { clsx, type ClassValue } from "clsx"; | ||
import { twMerge } from "tailwind-merge"; | ||
|
||
export function cn(...inputs: ClassValue[]) { | ||
return twMerge(clsx(inputs)); | ||
} | ||
|
||
export type WritersBlockSuggestion = { | ||
collectionName: string; | ||
suggestedName: string; | ||
tokenizedSuggestedName: string[]; | ||
}; | ||
|
||
export type WritersBlockName = { | ||
normalized_name: string; | ||
tokenized_name: string[]; | ||
}; | ||
|
||
export type WritersBlockCollection = { | ||
collection_id: string; | ||
collection_name: string; | ||
names: WritersBlockName[]; | ||
}; | ||
|
||
const getRandomElementOfArray = <Type>(array: Type[]): Type => | ||
array[Math.floor(Math.random() * array.length)]; | ||
|
||
const getRandomWritersBlockSuggestion = ( | ||
array: WritersBlockCollection[], | ||
): WritersBlockSuggestion => { | ||
const rawWritersBlockSuggestion = getRandomElementOfArray(array); | ||
const rawName = getRandomElementOfArray(rawWritersBlockSuggestion.names); | ||
return { | ||
collectionName: rawWritersBlockSuggestion.collection_name, | ||
suggestedName: rawName.normalized_name, | ||
tokenizedSuggestedName: rawName.tokenized_name, | ||
}; | ||
}; | ||
|
||
// TODO: move this function to namegraph-sdk | ||
export const sampleWritersBlockSuggestions = ( | ||
suggestionsCount: number, | ||
catalog?: WritersBlockCollection[], | ||
): WritersBlockSuggestion[] => { | ||
const uniqueCollectionsNames = new Set(); | ||
const uniqueSuggestionsNames = new Set(); | ||
const result = []; | ||
|
||
let collections = undefined; | ||
if (catalog?.length) collections = catalog; | ||
else collections = writersBlockSuggestions; | ||
|
||
while ( | ||
result.length !== suggestionsCount && | ||
result.length !== collections.length | ||
) { | ||
const writersBlockSuggestion = getRandomWritersBlockSuggestion(collections); | ||
|
||
if ( | ||
uniqueCollectionsNames.has(writersBlockSuggestion.collectionName) || | ||
uniqueSuggestionsNames.has(writersBlockSuggestion.suggestedName) | ||
) { | ||
continue; | ||
} else { | ||
uniqueCollectionsNames.add(writersBlockSuggestion.collectionName); | ||
uniqueSuggestionsNames.add(writersBlockSuggestion.suggestedName); | ||
result.push(writersBlockSuggestion); | ||
} | ||
} | ||
|
||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
# NameGraph SDK | ||
|
||
A TypeScript SDK for interacting with the NameGraph APIs, providing access to the world's largest collection of names. This SDK enables easy integration with the NameGraph API endpoints for name and collection suggestions. | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install namegraph-sdk | ||
# or | ||
yarn add namegraph-sdk | ||
``` | ||
|
||
## Quick Start | ||
|
||
```typescript | ||
import { createNameGraphClient } from "namegraph-sdk"; | ||
|
||
// Create a client instance | ||
const client = createNameGraphClient(); | ||
|
||
// Or use the default client | ||
import { namegraph } from "namegraph-sdk"; | ||
|
||
// Get name suggestions grouped by category | ||
const suggestions = await namegraph.groupedByCategory("zeus"); | ||
``` | ||
|
||
## Features | ||
|
||
- **Category-based Suggestions**: Get name suggestions organized by various categories including related terms, wordplay, alternates, and more | ||
- **Collection Management**: Find, count, and analyze collections of names | ||
- **Smart Sampling**: Sample and fetch top members from collections | ||
- **Advanced Search**: Search collections by string or by collection ID | ||
|
||
## API Reference | ||
|
||
### Client Configuration | ||
|
||
```typescript | ||
const client = createNameGraphClient({ | ||
namegraphEndpoint: "https://custom-api.endpoint.com", // Optional, defaults to https://api.namegraph.dev/ | ||
}); | ||
``` | ||
|
||
### Core Methods | ||
|
||
#### Group Names by Category | ||
|
||
```typescript | ||
const response = await client.groupedByCategory("zeus"); | ||
``` | ||
|
||
#### Get Suggestions by Category | ||
|
||
```typescript | ||
const suggestions = await client.suggestionsByCategory("zeus"); | ||
``` | ||
|
||
#### Sample Collection Members | ||
|
||
```typescript | ||
const members = await client.sampleCollectionMembers("collection_id"); | ||
``` | ||
|
||
#### Fetch Top Collection Members | ||
|
||
```typescript | ||
const topMembers = await client.fetchTopCollectionMembers("collection_id"); | ||
``` | ||
|
||
#### Find Collections | ||
|
||
```typescript | ||
// By string | ||
const collections = await client.findCollectionsByString("zeus god"); | ||
|
||
// By collection ID | ||
const related = await client.findCollectionsByCollection("collection_id"); | ||
|
||
// By member | ||
const memberCollections = await client.findCollectionsByMember("zeus"); | ||
``` | ||
|
||
#### Count Collections | ||
|
||
```typescript | ||
// By string | ||
const stringCount = await client.countCollectionsByString("zeus god"); | ||
|
||
// By member | ||
const memberCount = await client.countCollectionsByMember("zeus"); | ||
``` | ||
|
||
### Response Types | ||
|
||
The SDK provides TypeScript types for all API responses. Key types include: | ||
|
||
- `NameGraphSuggestion` | ||
- `NameGraphCategory` | ||
- `NameGraphCollection` | ||
- `NameGraphGroupedByCategoryResponse` | ||
- `NameGraphFetchTopCollectionMembersResponse` | ||
- `NameGraphCountCollectionsResponse` | ||
|
||
### Error Handling | ||
|
||
The SDK throws `NameGraphError` for API-related errors. Handle them appropriately: | ||
|
||
```typescript | ||
try { | ||
const suggestions = await client.groupedByCategory("example"); | ||
} catch (error) { | ||
if (error instanceof NameGraphError) { | ||
console.error(`API Error ${error.status}: ${error.message}`); | ||
} | ||
} | ||
``` | ||
|
||
### Request Cancellation | ||
|
||
Cancel ongoing requests using the `abortAllRequests` method: | ||
|
||
```typescript | ||
client.abortAllRequests(); | ||
``` | ||
|
||
## Categories | ||
|
||
The SDK supports various grouping categories for name suggestions: | ||
|
||
- `related`: Related terms and concepts | ||
- `wordplay`: Creative word variations | ||
- `alternates`: Alternative versions | ||
- `emojify`: Emoji-enhanced variations | ||
- `community`: Community-suggested names | ||
- `expand`: Expanded versions | ||
- `gowild`: Creative variations | ||
- `other`: Additional suggestions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.