Skip to content

Commit

Permalink
refactor: put the default host in a variable and reuse it
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AbdulrhmanGoni committed Jan 28, 2025
1 parent b95d44c commit c1af00e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
ShowResponse,
StatusResponse,
} from './interfaces.js'
import { defaultHost } from './defaultHost.js'

export class Ollama {
protected readonly config: Config
Expand All @@ -38,7 +39,7 @@ export class Ollama {
}

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
Expand Down
2 changes: 2 additions & 0 deletions src/defaultHost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const defaulPort = '11434';
export const defaultHost = `http://127.0.0.1:${defaulPort}`;
5 changes: 3 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { version } from './version.js'
import type { ErrorResponse, Fetch } from './interfaces.js'
import { defaulPort, defaultHost } from './defaultHost.js'

/**
* An error class for response errors.
Expand Down Expand Up @@ -268,7 +269,7 @@ export const parseJSON = async function* <T = unknown>(
*/
export const formatHost = (host: string): string => {
if (!host) {
return 'http://127.0.0.1:11434'
return defaultHost
}

let isExplicitProtocol = host.includes('://')
Expand All @@ -288,7 +289,7 @@ export const formatHost = (host: string): string => {
let port = url.port
if (!port) {
if (!isExplicitProtocol) {
port = '11434'
port = defaulPort
} else {
// Assign default ports based on the protocol
port = url.protocol === 'https:' ? '443' : '80'
Expand Down
3 changes: 2 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { describe, it, expect } from 'vitest'
import { formatHost } from '../src/utils'
import { defaultHost } from '../src/defaultHost'

describe('formatHost Function Tests', () => {
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', () => {
Expand Down

0 comments on commit c1af00e

Please sign in to comment.