@ffflorian/icanhazdadjoke 2.4.4 → 2.4.6

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/package.json CHANGED
@@ -2,16 +2,16 @@
2
2
  "author": "Florian Imdahl <git@ffflorian.de>",
3
3
  "bin": "dist/cli.js",
4
4
  "dependencies": {
5
- "@ffflorian/api-client": "2.2.0",
6
- "commander": "14.0.2"
5
+ "@ffflorian/api-client": "2.5.0",
6
+ "commander": "14.0.3"
7
7
  },
8
8
  "description": "An icanhazdadjoke.com API client with a CLI.",
9
9
  "devDependencies": {
10
10
  "@types/node": "~24",
11
- "rimraf": "6.1.2",
11
+ "rimraf": "6.1.3",
12
12
  "tsx": "4.21.0",
13
- "typedoc": "0.28.15",
14
- "typescript": "5.9.3"
13
+ "typedoc": "0.28.18",
14
+ "typescript": "6.0.2"
15
15
  },
16
16
  "engines": {
17
17
  "node": ">= 21"
@@ -38,6 +38,5 @@
38
38
  "start": "tsx src/cli.ts",
39
39
  "test": "exit 0"
40
40
  },
41
- "version": "2.4.4",
42
- "gitHead": "12bebfb90528bb894bccb99670373af33c6a5159"
41
+ "version": "2.4.6"
43
42
  }
@@ -1,13 +0,0 @@
1
- import { ICanHazDadJokeAPI } from './ICanHazDadJokeAPI';
2
- import type { ClientOptions } from './Interfaces';
3
- export declare class ICanHazDadJoke {
4
- readonly api: ICanHazDadJokeAPI;
5
- private readonly apiClient;
6
- private readonly options;
7
- constructor(options?: ClientOptions);
8
- /**
9
- * Set a new API URL.
10
- * @param url The new API URL.
11
- */
12
- setApiUrl(newURL: string): void;
13
- }
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ICanHazDadJoke = void 0;
4
- const api_client_1 = require("@ffflorian/api-client");
5
- const ICanHazDadJokeAPI_1 = require("./ICanHazDadJokeAPI");
6
- class ICanHazDadJoke {
7
- constructor(options) {
8
- this.options = Object.assign({ apiUrl: 'https://icanhazdadjoke.com' }, options);
9
- this.apiClient = new api_client_1.APIClient(this.options.apiUrl, {
10
- headers: {
11
- Accept: 'application/json',
12
- 'User-Agent': 'icanhazdadjoke.com API Client (https://github.com/ffflorian/api-clients/tree/main/packages/icanhazdadjoke)',
13
- },
14
- });
15
- this.api = new ICanHazDadJokeAPI_1.ICanHazDadJokeAPI(this.apiClient, this.options);
16
- }
17
- /**
18
- * Set a new API URL.
19
- * @param url The new API URL.
20
- */
21
- setApiUrl(newURL) {
22
- this.apiClient.setBaseURL(newURL);
23
- }
24
- }
25
- exports.ICanHazDadJoke = ICanHazDadJoke;
@@ -1,39 +0,0 @@
1
- import type { APIClient } from '@ffflorian/api-client';
2
- import type { ClientOptions, JokeResult, JokeResultWithImage, JokeSearchResult, RequestOptions, SearchOptions } from './Interfaces';
3
- export declare class ICanHazDadJokeAPI {
4
- protected readonly apiClient: APIClient;
5
- protected readonly options: ClientOptions;
6
- constructor(apiClient: APIClient, options: ClientOptions);
7
- /**
8
- * Fetch a dad joke by ID.
9
- * @see https://icanhazdadjoke.com/api#fetch-a-dad-joke
10
- */
11
- getById(id: string, options: {
12
- withImage: true;
13
- }): Promise<JokeResultWithImage>;
14
- getById(id: string, options?: RequestOptions): Promise<JokeResult>;
15
- /**
16
- * Fetch a dad joke by ID.
17
- * @see https://icanhazdadjoke.com/api#fetch-a-dad-joke
18
- */
19
- getByID(id: string, options: {
20
- withImage: true;
21
- }): Promise<JokeResultWithImage>;
22
- getByID(id: string, options?: RequestOptions): Promise<JokeResult>;
23
- /**
24
- * Fetch a random dad joke.
25
- * @see https://icanhazdadjoke.com/api#fetch-a-random-dad-joke
26
- */
27
- getRandom(options: {
28
- withImage: true;
29
- }): Promise<JokeResultWithImage>;
30
- getRandom(options?: RequestOptions): Promise<JokeResult>;
31
- /**
32
- * Search for dad jokes.
33
- * @param term search term to use (default: list all jokes)
34
- * @see https://icanhazdadjoke.com/api#search-for-dad-jokes
35
- */
36
- search(options: SearchOptions): Promise<JokeSearchResult>;
37
- search(query: string, options?: Omit<SearchOptions, 'term'>): Promise<JokeSearchResult>;
38
- private getImage;
39
- }
@@ -1,64 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.ICanHazDadJokeAPI = void 0;
13
- class ICanHazDadJokeAPI {
14
- constructor(apiClient, options) {
15
- this.apiClient = apiClient;
16
- this.options = options;
17
- }
18
- getById(id, options = {}) {
19
- return this.getByID(id, options);
20
- }
21
- getByID(id_1) {
22
- return __awaiter(this, arguments, void 0, function* (id, options = {}) {
23
- const { data: metaData } = yield this.apiClient.get(`/j/${id}`);
24
- if (options.withImage) {
25
- const imageData = yield this.getImage(`/j/${id}.png`);
26
- return Object.assign(Object.assign({}, metaData), { image: imageData });
27
- }
28
- return metaData;
29
- });
30
- }
31
- getRandom() {
32
- return __awaiter(this, arguments, void 0, function* (options = {}) {
33
- const { data: metaData } = yield this.apiClient.get('/');
34
- if (options.withImage) {
35
- const imageData = yield this.getImage(`/j/${metaData.id}.png`);
36
- return Object.assign(Object.assign({}, metaData), { image: imageData });
37
- }
38
- return metaData;
39
- });
40
- }
41
- search(query_1) {
42
- return __awaiter(this, arguments, void 0, function* (query, options = {}) {
43
- if (typeof query === 'string') {
44
- options.term = query;
45
- }
46
- const { data } = yield this.apiClient.get('/search', {
47
- params: options,
48
- });
49
- return data;
50
- });
51
- }
52
- getImage(imageUrl) {
53
- return __awaiter(this, void 0, void 0, function* () {
54
- const { data } = yield this.apiClient.get(imageUrl, {
55
- headers: {
56
- Accept: 'image/png',
57
- },
58
- responseType: 'arraybuffer',
59
- });
60
- return data;
61
- });
62
- }
63
- }
64
- exports.ICanHazDadJokeAPI = ICanHazDadJokeAPI;
@@ -1,33 +0,0 @@
1
- export interface RequestOptions {
2
- withImage?: boolean;
3
- }
4
- export interface ClientOptions {
5
- apiUrl?: string;
6
- }
7
- export interface JokeResult {
8
- id: string;
9
- joke: string;
10
- status: number;
11
- }
12
- export interface JokeSearchResult {
13
- current_page: number;
14
- limit: number;
15
- next_page: number;
16
- previous_page: number;
17
- results: JokeResult[];
18
- search_term: string;
19
- status: number;
20
- total_jokes: number;
21
- total_pages: number;
22
- }
23
- export interface JokeResultWithImage extends JokeResult {
24
- image: Buffer;
25
- }
26
- export interface SearchOptions {
27
- /** number of results to return per page (default: 20) (max: 30) */
28
- limit?: number;
29
- /** which page of results to fetch (default: 1) */
30
- page?: number;
31
- /** search term to use (default: list all jokes) */
32
- term?: string;
33
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
package/dist/cli.d.ts DELETED
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env node
2
- export {};
package/dist/cli.js DELETED
@@ -1,123 +0,0 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
- if (k2 === undefined) k2 = k;
5
- var desc = Object.getOwnPropertyDescriptor(m, k);
6
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
- desc = { enumerable: true, get: function() { return m[k]; } };
8
- }
9
- Object.defineProperty(o, k2, desc);
10
- }) : (function(o, m, k, k2) {
11
- if (k2 === undefined) k2 = k;
12
- o[k2] = m[k];
13
- }));
14
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
- Object.defineProperty(o, "default", { enumerable: true, value: v });
16
- }) : function(o, v) {
17
- o["default"] = v;
18
- });
19
- var __importStar = (this && this.__importStar) || (function () {
20
- var ownKeys = function(o) {
21
- ownKeys = Object.getOwnPropertyNames || function (o) {
22
- var ar = [];
23
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
- return ar;
25
- };
26
- return ownKeys(o);
27
- };
28
- return function (mod) {
29
- if (mod && mod.__esModule) return mod;
30
- var result = {};
31
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
- __setModuleDefault(result, mod);
33
- return result;
34
- };
35
- })();
36
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
37
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
38
- return new (P || (P = Promise))(function (resolve, reject) {
39
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
40
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
41
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
42
- step((generator = generator.apply(thisArg, _arguments || [])).next());
43
- });
44
- };
45
- Object.defineProperty(exports, "__esModule", { value: true });
46
- const commander_1 = require("commander");
47
- const fs_1 = require("fs");
48
- const path = __importStar(require("path"));
49
- const _1 = require("./");
50
- function init() {
51
- return __awaiter(this, arguments, void 0, function* (dir = '.') {
52
- const resolvedPath = path.resolve(dir);
53
- try {
54
- yield fs_1.promises.access(resolvedPath, fs_1.constants.F_OK | fs_1.constants.R_OK);
55
- const iCanHazDadJoke = new _1.ICanHazDadJoke();
56
- return [resolvedPath, iCanHazDadJoke];
57
- }
58
- catch (_a) {
59
- throw new Error('The specified path does not exist or is not writable.');
60
- }
61
- });
62
- }
63
- function save(filePath_1, imageResult_1) {
64
- return __awaiter(this, arguments, void 0, function* (filePath, imageResult, silent = false) {
65
- const { id, image } = imageResult;
66
- const resolvedFilePath = path.resolve(filePath, `dad_joke_${id}.png`);
67
- yield fs_1.promises.writeFile(resolvedFilePath, image);
68
- if (!silent) {
69
- console.info(`Saved image to "${resolvedFilePath}".`);
70
- }
71
- });
72
- }
73
- const { description, name, version } = require('../package.json');
74
- commander_1.program.on('command:*', () => commander_1.program.help());
75
- commander_1.program
76
- .name(name.replace(/^@[^/]+\//, ''))
77
- .version(version, '-v, --version')
78
- .description(description)
79
- .option('-i, --image', 'Save the joke as image')
80
- .option('-o, --output <dir>', 'Specify the output directory', path.resolve('.'))
81
- .option('-s, --silent', `Don't output save messages`);
82
- commander_1.program
83
- .command('random')
84
- .description('Fetch a random joke')
85
- .action(() => __awaiter(void 0, void 0, void 0, function* () {
86
- const options = commander_1.program.opts();
87
- try {
88
- const [resolvedPath, iCanHazDadJoke] = yield init(options.output);
89
- const result = yield iCanHazDadJoke.api.getRandom({ withImage: !!options.image });
90
- console.info(result.joke);
91
- if (options.image) {
92
- yield save(resolvedPath, result, !!options.silent);
93
- }
94
- }
95
- catch (error) {
96
- console.error(`Error: ${error.message}`);
97
- process.exit(1);
98
- }
99
- }));
100
- commander_1.program
101
- .command('id')
102
- .argument('<id>', 'The joke ID')
103
- .description('Fetch joke by ID')
104
- .action((id) => __awaiter(void 0, void 0, void 0, function* () {
105
- const options = commander_1.program.opts();
106
- try {
107
- const [resolvedPath, iCanHazDadJoke] = yield init(options.output);
108
- const result = yield iCanHazDadJoke.api.getById(id, { withImage: !!options.image });
109
- console.info(result.joke);
110
- if (options.image) {
111
- yield save(resolvedPath, result, !!options.silent);
112
- }
113
- }
114
- catch (error) {
115
- console.error(`Error: ${error.message}`);
116
- process.exit(1);
117
- }
118
- }));
119
- commander_1.program.parse(process.argv);
120
- if (!process.argv.slice(2).length) {
121
- commander_1.program.outputHelp();
122
- process.exit(1);
123
- }
package/dist/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './ICanHazDadJoke';
2
- export * from './Interfaces';
package/dist/index.js DELETED
@@ -1,18 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./ICanHazDadJoke"), exports);
18
- __exportStar(require("./Interfaces"), exports);