Browse Source

refactor: put the default host in a variable and then reuse it (#197)

The default host 'http://127.0.0.1:11434' is hardcoded in
multiple places, So, Put it in one variable in a central place
(`src/defaultHost.ts` file) and reuse it in those places.
Abdulrahman Goni 3 tháng trước cách đây
mục cha
commit
9ff1621535
6 tập tin đã thay đổi với 293 bổ sung224 xóa
  1. 282 218
      package-lock.json
  2. 2 2
      package.json
  3. 2 1
      src/browser.ts
  4. 2 0
      src/constant.ts
  5. 3 2
      src/utils.ts
  6. 2 1
      test/index.test.ts

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 282 - 218
package-lock.json


+ 2 - 2
package.json

@@ -38,10 +38,10 @@
     "@typescript-eslint/eslint-plugin": "^5.42.1",
     "@typescript-eslint/eslint-plugin": "^5.42.1",
     "@typescript-eslint/parser": "^5.42.1",
     "@typescript-eslint/parser": "^5.42.1",
     "eslint": "^8.29.0",
     "eslint": "^8.29.0",
-    "vitest": "^2.1.6",
     "prettier": "^3.2.4",
     "prettier": "^3.2.4",
     "typescript": "^5.3.2",
     "typescript": "^5.3.2",
-    "unbuild": "^2.0.0"
+    "unbuild": "^2.0.0",
+    "vitest": "^3.0.4"
   },
   },
   "dependencies": {
   "dependencies": {
     "whatwg-fetch": "^3.6.20"
     "whatwg-fetch": "^3.6.20"

+ 2 - 1
src/browser.ts

@@ -25,6 +25,7 @@ import type {
   ShowResponse,
   ShowResponse,
   StatusResponse,
   StatusResponse,
 } from './interfaces.js'
 } from './interfaces.js'
+import { defaultHost } from './constant.js'
 
 
 export class Ollama {
 export class Ollama {
   protected readonly config: Config
   protected readonly config: Config
@@ -38,7 +39,7 @@ export class Ollama {
     }
     }
 
 
     if (!config?.proxy) {
     if (!config?.proxy) {
-      this.config.host = utils.formatHost(config?.host ?? 'http://127.0.0.1:11434')
+      this.config.host = utils.formatHost(config?.host ?? defaultHost)
     }
     }
 
 
     this.fetch = config?.fetch ?? fetch
     this.fetch = config?.fetch ?? fetch

+ 2 - 0
src/constant.ts

@@ -0,0 +1,2 @@
+export const defaultPort = '11434';
+export const defaultHost = `http://127.0.0.1:${defaultPort}`;

+ 3 - 2
src/utils.ts

@@ -1,5 +1,6 @@
 import { version } from './version.js'
 import { version } from './version.js'
 import type { ErrorResponse, Fetch } from './interfaces.js'
 import type { ErrorResponse, Fetch } from './interfaces.js'
+import { defaultPort, defaultHost } from './constant.js'
 
 
 /**
 /**
  * An error class for response errors.
  * An error class for response errors.
@@ -268,7 +269,7 @@ export const parseJSON = async function* <T = unknown>(
  */
  */
 export const formatHost = (host: string): string => {
 export const formatHost = (host: string): string => {
   if (!host) {
   if (!host) {
-    return 'http://127.0.0.1:11434'
+    return defaultHost
   }
   }
 
 
   let isExplicitProtocol = host.includes('://')
   let isExplicitProtocol = host.includes('://')
@@ -288,7 +289,7 @@ export const formatHost = (host: string): string => {
   let port = url.port
   let port = url.port
   if (!port) {
   if (!port) {
     if (!isExplicitProtocol) {
     if (!isExplicitProtocol) {
-      port = '11434'
+      port = defaultPort
     } else {
     } else {
       // Assign default ports based on the protocol
       // Assign default ports based on the protocol
       port = url.protocol === 'https:' ? '443' : '80'
       port = url.protocol === 'https:' ? '443' : '80'

+ 2 - 1
test/index.test.ts

@@ -1,9 +1,10 @@
 import { describe, it, expect } from 'vitest'
 import { describe, it, expect } from 'vitest'
 import { formatHost } from '../src/utils'
 import { formatHost } from '../src/utils'
+import { defaultHost } from '../src/constant'
 
 
 describe('formatHost Function Tests', () => {
 describe('formatHost Function Tests', () => {
   it('should return default URL for empty string', () => {
   it('should return default URL for empty string', () => {
-    expect(formatHost('')).toBe('http://127.0.0.1:11434')
+    expect(formatHost('')).toBe(defaultHost)
   })
   })
 
 
   it('should parse plain IP address', () => {
   it('should parse plain IP address', () => {

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác