Skip to content

Commit 8979841

Browse files
committed
refactor(api.ts): reformat imports to be grouped and sorted alphabetically
refactor(api.ts): replace axios with execa to get the latest version of opencommit from npm registry
1 parent 1e97408 commit 8979841

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/api.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ import {
77
OpenAIApi
88
} from 'openai';
99

10-
import {CONFIG_MODES, DEFAULT_MODEL_TOKEN_LIMIT, getConfig} from './commands/config';
11-
import {tokenCount} from './utils/tokenCount';
12-
import {GenerateCommitMessageErrorEnum} from './generateCommitMessageFromGitDiff';
10+
import {
11+
CONFIG_MODES,
12+
DEFAULT_MODEL_TOKEN_LIMIT,
13+
getConfig
14+
} from './commands/config';
15+
import { tokenCount } from './utils/tokenCount';
16+
import { GenerateCommitMessageErrorEnum } from './generateCommitMessageFromGitDiff';
17+
import { execa } from 'execa';
1318

1419
const config = getConfig();
1520

@@ -58,11 +63,11 @@ class OpenAi {
5863
max_tokens: maxTokens || 500
5964
};
6065
try {
61-
const REQUEST_TOKENS = messages.map(
62-
(msg) => tokenCount(msg.content) + 4
63-
).reduce((a, b) => a + b, 0);
66+
const REQUEST_TOKENS = messages
67+
.map((msg) => tokenCount(msg.content) + 4)
68+
.reduce((a, b) => a + b, 0);
6469

65-
if (REQUEST_TOKENS > (DEFAULT_MODEL_TOKEN_LIMIT - maxTokens)) {
70+
if (REQUEST_TOKENS > DEFAULT_MODEL_TOKEN_LIMIT - maxTokens) {
6671
throw new Error(GenerateCommitMessageErrorEnum.tooMuchTokens);
6772
}
6873

@@ -98,10 +103,8 @@ export const getOpenCommitLatestVersion = async (): Promise<
98103
string | undefined
99104
> => {
100105
try {
101-
const { data } = await axios.get(
102-
'https://unpkg.com/opencommit/package.json'
103-
);
104-
return data.version;
106+
const { stdout } = await execa('npm', ['view', 'opencommit', 'version']);
107+
return stdout;
105108
} catch (_) {
106109
outro('Error while getting the latest version of opencommit');
107110
return undefined;

0 commit comments

Comments
 (0)