tdk-api-wrapper 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/cli.ts CHANGED
@@ -1,15 +1,61 @@
1
1
  #!/usr/bin/env node
2
2
  import { TDK } from "./tdk";
3
3
 
4
- const args = process.argv.slice(2);
5
- const command = args[0];
6
- const word = args[1];
4
+ const rawArgs = process.argv.slice(2);
5
+ const jsonMode = rawArgs.includes("--json");
6
+ const args = rawArgs.filter((a) => a !== "--json");
7
+ const KNOWN_COMMANDS = new Set([
8
+ "ara",
9
+ "anlam",
10
+ "koken",
11
+ "ornek",
12
+ "hece",
13
+ "uyum",
14
+ "yazim",
15
+ "gunun",
16
+ "rastgele",
17
+ "esanlam",
18
+ "karsit",
19
+ "yabanci",
20
+ "kurallar",
21
+ "kural",
22
+ "karsilastir",
23
+ "analiz",
24
+ "oneri",
25
+ ]);
26
+
27
+ let command = args[0];
28
+ let word = args.slice(1).join(" ");
29
+
30
+ if (command && !KNOWN_COMMANDS.has(command) && command !== "--help" && command !== "-h") {
31
+ word = args.join(" ");
32
+ command = "anlam";
33
+ }
34
+
35
+ function printResult(data: unknown, formatted: () => void) {
36
+ if (jsonMode) {
37
+ console.log(JSON.stringify(data));
38
+ } else {
39
+ formatted();
40
+ }
41
+ }
42
+
43
+ function printError(message: string) {
44
+ if (jsonMode) {
45
+ console.log(JSON.stringify({ error: message }));
46
+ } else {
47
+ console.log(`Hata: ${message}`);
48
+ }
49
+ }
7
50
 
8
51
  async function run() {
9
- if (!command) {
10
- console.log("Kullanım: tdk <komut> <kelime>");
11
- console.log("Komutlar: ara, anlam, koken, ornek, hece, uyum, yazim");
12
- process.exit(1);
52
+ if (!command || command === "--help" || command === "-h") {
53
+ console.log("Kullanım: tdk [komut] <kelime> [--json]");
54
+ console.log(
55
+ "Komutlar: ara, anlam, koken, ornek, hece, uyum, yazim, gunun, rastgele, esanlam, karsit, yabanci, kurallar, kural, karsilastir, analiz, oneri"
56
+ );
57
+ console.log("Not: Komut belirtilmezse doğrudan kelime anlamı aranır (örn: tdk selam)");
58
+ process.exit(command ? 0 : 1);
13
59
  }
14
60
 
15
61
  TDK.enableCache(false);
@@ -17,63 +63,208 @@ async function run() {
17
63
  try {
18
64
  switch (command) {
19
65
  case "ara":
20
- case "anlam":
66
+ case "anlam": {
21
67
  if (!word) throw new Error("Kelime belirtmelisiniz.");
22
68
  const meanings = await TDK.getMeanings(word);
23
- if (meanings.length === 0) {
24
- console.log("Sonuç bulunamadı.");
25
- } else {
26
- meanings.forEach((m, i) => console.log(`${i + 1}. ${m}`));
27
- }
69
+ printResult(meanings, () => {
70
+ if (meanings.length === 0) {
71
+ console.log("Sonuç bulunamadı.");
72
+ } else {
73
+ meanings.forEach((m, i) => console.log(`${i + 1}. ${m}`));
74
+ }
75
+ });
28
76
  break;
77
+ }
29
78
 
30
- case "koken":
79
+ case "koken": {
31
80
  if (!word) throw new Error("Kelime belirtmelisiniz.");
32
81
  const origin = await TDK.getOrigin(word);
33
- console.log(`Köken: ${origin}`);
82
+ printResult({ word, origin }, () => console.log(`Köken: ${origin}`));
34
83
  break;
84
+ }
35
85
 
36
- case "ornek":
86
+ case "ornek": {
37
87
  if (!word) throw new Error("Kelime belirtmelisiniz.");
38
88
  const examples = await TDK.getExamples(word);
39
- if (examples.length === 0) {
40
- console.log("Örnek bulunamadı.");
41
- } else {
42
- examples.forEach((ex, i) => {
43
- const yazar = ex.author ? ` (${ex.author})` : "";
44
- console.log(`${i + 1}. ${ex.sentence}${yazar}`);
45
- });
46
- }
89
+ printResult(examples, () => {
90
+ if (examples.length === 0) {
91
+ console.log("Örnek bulunamadı.");
92
+ } else {
93
+ examples.forEach((ex, i) => {
94
+ const yazar = ex.author ? ` (${ex.author})` : "";
95
+ console.log(`${i + 1}. ${ex.sentence}${yazar}`);
96
+ });
97
+ }
98
+ });
47
99
  break;
100
+ }
48
101
 
49
- case "hece":
102
+ case "hece": {
50
103
  if (!word) throw new Error("Kelime belirtmelisiniz.");
51
104
  const syllables = TDK.syllabicate(word);
52
- console.log(`Heceler: ${syllables.join("-")}`);
105
+ printResult(syllables, () => console.log(`Heceler: ${syllables.join("-")}`));
53
106
  break;
107
+ }
54
108
 
55
- case "uyum":
109
+ case "uyum": {
56
110
  if (!word) throw new Error("Kelime belirtmelisiniz.");
57
111
  const isHarmony = TDK.checkVowelHarmony(word);
58
- console.log(`Büyük Ünlü Uyumu: ${isHarmony ? "Uyar" : "Uymaz"}`);
112
+ printResult({ word, harmony: isHarmony }, () =>
113
+ console.log(`Büyük Ünlü Uyumu: ${isHarmony ? "Uyar" : "Uymaz"}`)
114
+ );
59
115
  break;
116
+ }
60
117
 
61
- case "yazim":
118
+ case "yazim": {
62
119
  if (!word) throw new Error("Kelime belirtmelisiniz.");
63
120
  const spellResult = await TDK.checkSpelling(word);
64
- if (spellResult.isCorrect) {
65
- console.log("Doğru yazım.");
66
- } else {
67
- console.log(`Yanlış yazım.${spellResult.suggestion ? " Doğrusu: " + spellResult.suggestion : ""}`);
68
- }
121
+ printResult(spellResult, () => {
122
+ if (spellResult.isCorrect) {
123
+ console.log("Doğru yazım.");
124
+ } else {
125
+ console.log(`Yanlış yazım.${spellResult.suggestion ? " Doğrusu: " + spellResult.suggestion : ""}`);
126
+ }
127
+ });
128
+ break;
129
+ }
130
+
131
+ case "gunun": {
132
+ const wotd = await TDK.getWordOfTheDay();
133
+ printResult(wotd, () => {
134
+ if (!wotd) {
135
+ console.log("Günün kelimesi alınamadı.");
136
+ } else {
137
+ console.log(`Günün kelimesi: ${wotd.word}`);
138
+ wotd.meanings.forEach((m, i) => console.log(`${i + 1}. ${m}`));
139
+ }
140
+ });
141
+ break;
142
+ }
143
+
144
+ case "rastgele": {
145
+ const pick = await TDK.getRandomWord();
146
+ printResult(pick, () => {
147
+ if (!pick) {
148
+ console.log("Rastgele içerik alınamadı.");
149
+ } else {
150
+ const label = pick.type === "kelime" ? "Kelime" : "Atasözü";
151
+ console.log(`${label}: ${pick.madde}`);
152
+ console.log(pick.anlam);
153
+ }
154
+ });
155
+ break;
156
+ }
157
+
158
+ case "esanlam": {
159
+ if (!word) throw new Error("Kelime belirtmelisiniz.");
160
+ const synonyms = await TDK.getSynonyms(word);
161
+ printResult(synonyms, () => {
162
+ if (synonyms.length === 0) {
163
+ console.log("Eş anlamlı kelime bulunamadı.");
164
+ } else {
165
+ synonyms.forEach((s, i) => console.log(`${i + 1}. ${s}`));
166
+ }
167
+ });
168
+ break;
169
+ }
170
+
171
+ case "karsit": {
172
+ if (!word) throw new Error("Kelime belirtmelisiniz.");
173
+ const antonyms = await TDK.getAntonyms(word);
174
+ printResult(antonyms, () => {
175
+ if (antonyms.length === 0) {
176
+ console.log("Zıt anlamlı kelime bulunamadı.");
177
+ } else {
178
+ antonyms.forEach((s, i) => console.log(`${i + 1}. ${s}`));
179
+ }
180
+ });
181
+ break;
182
+ }
183
+
184
+ case "yabanci": {
185
+ if (!word) throw new Error("Kelime belirtmelisiniz.");
186
+ const foreign = await TDK.isForeignWord(word);
187
+ printResult({ word, foreign }, () => {
188
+ if (foreign === null) {
189
+ console.log("Kelime bulunamadı.");
190
+ } else {
191
+ console.log(foreign ? "Yabancı kökenli." : "Türkçe kökenli.");
192
+ }
193
+ });
194
+ break;
195
+ }
196
+
197
+ case "kurallar": {
198
+ const rules = await TDK.getKurallar();
199
+ printResult(rules, () => {
200
+ if (rules.length === 0) {
201
+ console.log("Kural listesi alınamadı.");
202
+ } else {
203
+ rules.forEach((r, i) => console.log(`${i + 1}. ${r.adi}`));
204
+ }
205
+ });
206
+ break;
207
+ }
208
+
209
+ case "kural": {
210
+ if (!word) throw new Error("Kural adı belirtmelisiniz.");
211
+ const rule = await TDK.getRule(word);
212
+ printResult(rule, () => {
213
+ console.log(rule ?? "Kural bulunamadı.");
214
+ });
215
+ break;
216
+ }
217
+
218
+ case "karsilastir": {
219
+ const [wordA, wordB] = args.slice(1);
220
+ if (!wordA || !wordB) throw new Error("İki kelime belirtmelisiniz.");
221
+ const comparison = await TDK.compareWords(wordA, wordB);
222
+ printResult(comparison, () => {
223
+ for (const side of [comparison.a, comparison.b]) {
224
+ console.log(`${side.word}: ${side.meaningCount} anlam, köken: ${side.origin ?? "bulunamadı"}, hece: ${side.syllables.join("-")}, büyük ünlü uyumu: ${side.harmony ? "uyar" : "uymaz"}`);
225
+ }
226
+ });
227
+ break;
228
+ }
229
+
230
+ case "analiz": {
231
+ if (!word) throw new Error("Metin belirtmelisiniz.");
232
+ const analysis = await TDK.analyzeText(word);
233
+ printResult(analysis, () => {
234
+ if (analysis.length === 0) {
235
+ console.log("Analiz edilecek kelime bulunamadı.");
236
+ } else {
237
+ analysis.forEach((a) => {
238
+ if (a.found) {
239
+ console.log(`${a.word}: ${a.meaning ?? "-"} (${a.origin})`);
240
+ } else {
241
+ console.log(`${a.word}: bulunamadı`);
242
+ }
243
+ });
244
+ }
245
+ });
246
+ break;
247
+ }
248
+
249
+ case "oneri": {
250
+ if (!word) throw new Error("Önek belirtmelisiniz.");
251
+ const suggestions = await TDK.getSuggestions(word);
252
+ printResult(suggestions, () => {
253
+ if (suggestions.length === 0) {
254
+ console.log("Öneri bulunamadı.");
255
+ } else {
256
+ suggestions.forEach((s, i) => console.log(`${i + 1}. ${s}`));
257
+ }
258
+ });
69
259
  break;
260
+ }
70
261
 
71
262
  default:
72
- console.log("Bilinmeyen komut.");
263
+ printError("Bilinmeyen komut.");
73
264
  }
74
265
  } catch (error) {
75
266
  if (error instanceof Error) {
76
- console.log(`Hata: ${error.message}`);
267
+ printError(error.message);
77
268
  }
78
269
  }
79
270
  }
package/src/errors.ts ADDED
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Base class for all errors thrown by this library.
3
+ */
4
+ export class TDKError extends Error {
5
+ constructor(message: string) {
6
+ super(message);
7
+ this.name = "TDKError";
8
+ Object.setPrototypeOf(this, new.target.prototype);
9
+ }
10
+ }
11
+
12
+ /**
13
+ * Thrown when a caller-supplied argument is invalid (e.g. an empty word).
14
+ */
15
+ export class TDKValidationError extends TDKError {
16
+ constructor(message: string) {
17
+ super(message);
18
+ this.name = "TDKValidationError";
19
+ Object.setPrototypeOf(this, new.target.prototype);
20
+ }
21
+ }
22
+
23
+ /**
24
+ * Thrown when a request to sozluk.gov.tr fails at the network/HTTP level
25
+ * (connection failure, non-OK HTTP status, unparsable response, etc).
26
+ */
27
+ export class TDKNetworkError extends TDKError {
28
+ public readonly status?: number;
29
+ public readonly cause?: unknown;
30
+
31
+ constructor(message: string, options?: { status?: number; cause?: unknown }) {
32
+ super(message);
33
+ this.name = "TDKNetworkError";
34
+ this.status = options?.status;
35
+ this.cause = options?.cause;
36
+ Object.setPrototypeOf(this, new.target.prototype);
37
+ }
38
+ }
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export { TDK } from "./tdk";
2
2
  export * from "./types";
3
+ export * from "./errors";