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

js lib user agent headers #46

Merged
merged 2 commits into from
Feb 16, 2024
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
2 changes: 2 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jobs:
registry-url: https://registry.npmjs.org
cache: npm
- run: npm ci
- name: Set version in src/version.js
run: echo "export const version = '${GITHUB_REF_NAME#v}';" > src/version.js
- run: |
npm version --no-git-tag-version ${GITHUB_REF_NAME#v}
npm publish
Expand Down
41 changes: 37 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { version } from './version'
import type { Fetch, ErrorResponse } from './interfaces.js'

class ResponseError extends Error {
Expand Down Expand Up @@ -40,16 +41,48 @@
}
}

function getPlatform() {
if (typeof window !== 'undefined' && window.navigator) {
return `${window.navigator.platform.toLowerCase()} Browser/${navigator.userAgent};`
} else if (typeof process !== 'undefined') {
return `${process.arch} ${process.platform} Node.js/${process.version}`
}
return '' // unknown
}

const fetchWithHeaders = async (
fetch: Fetch,
url: string,
options: RequestInit = {},
): Promise<Response> => {
const defaultHeaders = {
'Content-Type': 'application/json',
Accept: 'application/json',
'User-Agent': `ollama-js/${version} (${getPlatform()})`,
}

if (!options.headers) {
options.headers = {}
}

options.headers = {
...defaultHeaders,
...options.headers,
}

return fetch(url, options)
}

export const get = async (fetch: Fetch, host: string): Promise<Response> => {
const response = await fetch(host)
const response = await fetchWithHeaders(fetch, host)

await checkOk(response)

return response
}

export const head = async (fetch: Fetch, host: string): Promise<Response> => {
const response = await fetch(host, {
const response = await fetchWithHeaders(fetch, host, {
method: 'HEAD',
})

Expand All @@ -64,13 +97,13 @@
data?: Record<string, unknown> | BodyInit,
options?: { signal: AbortSignal },
): Promise<Response> => {
const isRecord = (input: any): input is Record<string, unknown> => {

Check warning on line 100 in src/utils.ts

View workflow job for this annotation

GitHub Actions / test (16)

Unexpected any. Specify a different type

Check warning on line 100 in src/utils.ts

View workflow job for this annotation

GitHub Actions / test (18)

Unexpected any. Specify a different type

Check warning on line 100 in src/utils.ts

View workflow job for this annotation

GitHub Actions / test (20)

Unexpected any. Specify a different type
return input !== null && typeof input === 'object' && !Array.isArray(input)
}

const formattedData = isRecord(data) ? JSON.stringify(data) : data

const response = await fetch(host, {
const response = await fetchWithHeaders(fetch, host, {
method: 'POST',
body: formattedData,
signal: options?.signal,
Expand All @@ -86,7 +119,7 @@
host: string,
data?: Record<string, unknown>,
): Promise<Response> => {
const response = await fetch(host, {
const response = await fetchWithHeaders(fetch, host, {
method: 'DELETE',
body: JSON.stringify(data),
})
Expand Down
1 change: 1 addition & 0 deletions src/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const version = '0.0.0'
Loading