Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions scripts/generate-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ function generateClientCode(namespaces: Map<string, OperationInfo[]>): string {
allFunctions.sort();

// Generate imports
const imports = `import {
const imports = `import packageJson from '../package.json';
import {
client,
${allFunctions.map(f => ` ${f},`).join('\n')}
} from './generated/sdk.gen';
Expand Down Expand Up @@ -332,19 +333,21 @@ ${namespaceProps.join('\n\n')}
this.baseURL = options.baseURL ?? 'https://zernio.com/api';
this._options = options;

// Configure the generated client
// Configure the generated client. User-Agent and defaultHeaders are
// applied at config time (not via the interceptor) because Node 20's
// undici treats User-Agent as a forbidden header on already-constructed
// Request objects, silently dropping \`headers.set('User-Agent', …)\`.
client.setConfig({
baseUrl: this.baseURL,
headers: {
'User-Agent': \`zernio-node/\${packageJson.version}\`,
...(options.defaultHeaders ?? {}),
},
});

// Add auth interceptor
client.interceptors.request.use((request) => {
request.headers.set('Authorization', \`Bearer \${this.apiKey}\`);
if (options.defaultHeaders) {
for (const [key, value] of Object.entries(options.defaultHeaders)) {
request.headers.set(key, value);
}
}
return request;
});

Expand Down
15 changes: 9 additions & 6 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import packageJson from '../package.json';
import {
client,
activateSequence,
Expand Down Expand Up @@ -965,19 +966,21 @@ export class Zernio {
this.baseURL = options.baseURL ?? 'https://zernio.com/api';
this._options = options;

// Configure the generated client
// Configure the generated client. User-Agent and defaultHeaders are
// applied at config time (not via the interceptor) because Node 20's
// undici treats User-Agent as a forbidden header on already-constructed
// Request objects, silently dropping `headers.set('User-Agent', …)`.
client.setConfig({
baseUrl: this.baseURL,
headers: {
'User-Agent': `zernio-node/${packageJson.version}`,
...(options.defaultHeaders ?? {}),
},
});

// Add auth interceptor
client.interceptors.request.use((request) => {
request.headers.set('Authorization', `Bearer ${this.apiKey}`);
if (options.defaultHeaders) {
for (const [key, value] of Object.entries(options.defaultHeaders)) {
request.headers.set(key, value);
}
}
return request;
});

Expand Down
40 changes: 40 additions & 0 deletions tests/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect } from 'vitest';
import Zernio, { Late, ZernioApiError, LateApiError } from '../src';
import packageJson from '../package.json';

describe('Zernio Client', () => {
it('should throw error when no API key is provided', () => {
Expand Down Expand Up @@ -72,6 +73,45 @@ describe('Zernio Client', () => {
expect(client.baseURL).toBe('https://custom.example.com/api');
});

it('should include the SDK version in the user agent', async () => {
const client = new Zernio({ apiKey: 'test_key' });
let request: Request | undefined;

await client.posts.listPosts({
fetch: async (input) => {
request = input instanceof Request ? input : new Request(input);
return new Response('{}', {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
},
});

expect(request?.headers.get('User-Agent')).toBe(`zernio-node/${packageJson.version}`);
});

it('should allow the user agent to be overridden', async () => {
const client = new Zernio({
apiKey: 'test_key',
defaultHeaders: {
'User-Agent': 'custom-agent',
},
});
let request: Request | undefined;

await client.posts.listPosts({
fetch: async (input) => {
request = input instanceof Request ? input : new Request(input);
return new Response('{}', {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
},
});

expect(request?.headers.get('User-Agent')).toBe('custom-agent');
});

it('should have all resource namespaces', () => {
const client = new Zernio({ apiKey: 'test_key' });

Expand Down
Loading