浏览代码

js lib user agent headers (#46)

Bruce MacDonald 1 年之前
父节点
当前提交
3918f13c00
共有 3 个文件被更改,包括 40 次插入4 次删除
  1. 2 0
      .github/workflows/publish.yaml
  2. 37 4
      src/utils.ts
  3. 1 0
      src/version.ts

+ 2 - 0
.github/workflows/publish.yaml

@@ -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

+ 37 - 4
src/utils.ts

@@ -1,3 +1,4 @@
+import { version } from './version'
 import type { Fetch, ErrorResponse } from './interfaces.js'
 
 class ResponseError extends Error {
@@ -40,8 +41,40 @@ const checkOk = async (response: Response): Promise<void> => {
   }
 }
 
+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)
 
@@ -49,7 +82,7 @@ export const get = async (fetch: Fetch, host: string): Promise<Response> => {
 }
 
 export const head = async (fetch: Fetch, host: string): Promise<Response> => {
-  const response = await fetch(host, {
+  const response = await fetchWithHeaders(fetch, host, {
     method: 'HEAD',
   })
 
@@ -70,7 +103,7 @@ export const post = async (
 
   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,
@@ -86,7 +119,7 @@ export const del = async (
   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),
   })

+ 1 - 0
src/version.ts

@@ -0,0 +1 @@
+export const version = '0.0.0'