Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug]: Property 'generateSecuredApiKey' does not exist on type 'Algoliasearch' #1583

Open
remihuigen opened this issue Mar 17, 2025 · 8 comments
Labels

Comments

@remihuigen
Copy link

Description

type definitions are missing for generateSecuredApiKey. This issue was also metioned in #1548

import { algoliasearch } from 'algoliasearch'
const client = algoliasearch('applicationId', 'adminApiKey')
const securedKey = client.generateSecuredApiKey({
  parentApiKey: 'publicApiKey',
  restrictions: {
    filters: 'visible_by:public'
  }
})
Property 'generateSecuredApiKey' does not exist on type 'Algoliasearch'.ts(2339)

Client

Search

Version

5.21.0

Relevant log output

@remihuigen remihuigen added the bug label Mar 17, 2025
@shortcuts
Copy link
Member

shortcuts commented Mar 17, 2025

Hey, on what environment is your application running? generateSecuredApiKey is only available from the node bundle, or for more modern frontend implementations fetch also offers it. If you are in a cloudflare worker or similar, the worker build also provides an implementation.

@remihuigen
Copy link
Author

Well the ts error is popping up in local development. I'm using it in a nitro server route

The method itself works, both in node environment and in CF workers. So its just the types that are missing.
Image

.

@shortcuts
Copy link
Member

Would you mind providing a minimal reproduction if possible? From a simple node env it seems like the type is present https://github.com/algolia/api-clients-automation/blob/main/playground/javascript/node/algoliasearch.ts#L32 so I assume this is a resolution problem

@shortcuts
Copy link
Member

Looking at nitrojs/nitro#2242 which points to https://github.com/nitrojs/nitro/blob/7e13a51fe6cf005109fd5ab60015cb1b38380be9/src/options.ts#L542-L552, it seems like worker should resolve properly, but from the issue details, it might resolve to browser instead

@shortcuts
Copy link
Member

Could you log client.transporter.algoliaAgent.value in your environment and show what it returns? the user agent should contain the build used

@remihuigen
Copy link
Author

log output of client.transporter.algoliaAgent.value:
Algolia for JavaScript (5.21.0); Search (5.21.0); Node.js (22.14.0)

See reproduction: https://stackblitz.com/edit/nuxt-starter-l3ypukxx?file=modules%2Fsearch%2Findex.ts

After installing the deps, line 19 of ./modules/search/index gives the typescript error

Please note that stackblitz node version is 18. I'm using v22 (as per logs above).

@shortcuts
Copy link
Member

Pretty weird to see this UA as hovering the import shows the browser one: module "node_modules/.pnpm/[email protected]/node_modules/algoliasearch/dist/browser"

Image

@benyaminl
Copy link

benyaminl commented Mar 21, 2025

@shortcuts I also found this happening to NextJS with App Route in import { algoliasearch } from "algoliasearch/dist/node";

https://nextjs.org/docs/app/building-your-application/routing/route-handlers#convention

import { algoliasearch } from "algoliasearch/dist/node";

export async function GET() {
    // const algoliasearch = require('algoliasearch');
    const APP_KEY = 'RANDOM_KEY';
    const client = algoliasearch(APP_KEY, 'HAIYAH_WHAT_HAPPEND');

    const securedAppKey = client.generateSecuredApiKey('HAIYAH_WHAT_HAPPEND', {
        restrictIndices: 'demo_ecommerce',
    })

    return Response.json({ appKey: APP_KEY, securedAppKey });
}

Image

I do see that https://github.com/algolia/algoliasearch-client-javascript/blob/main/packages/client-search/builds/node.ts#L59 It's available on node.

But upon resolving to /dist/node, it doesn't have the type helper of it. (or is it intended?)

EDIT: Strange thing is, other developer say it's available? #1548 (comment)

Or is there anyway around for now so using App Route or anything fetch we can get the secured generated one time API Key?

EDIT 2: For now I use work around with server side rendering using PHP/Composer package, at least it work on the PHP. Hope this resolved soon or we can have different importing ways into typescript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants