@ffflorian/icanhazdadjoke 2.4.7 → 2.6.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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ICanHazDadJoke [![npm version](https://img.shields.io/npm/v/@ffflorian/icanhazdadjoke.svg)](https://www.npmjs.com/package/@ffflorian/icanhazdadjoke)
2
2
 
3
- An [ICanHazDadJoke](https://icanhazdadjoke.com) API client with a CLI.
3
+ An [ICanHazDadJoke](https://icanhazdadjoke.com) API client with a CLI. For a documentation on the API see [the icanhazdadjoke API reference](https://icanhazdadjoke.com/api).
4
4
 
5
5
  ## Usage
6
6
 
@@ -1,12 +1,11 @@
1
1
  import type { APIClient } from '@ffflorian/api-client';
2
- import type { ClientOptions, JokeResult, JokeResultWithImage, JokeSearchResult, RequestOptions, SearchOptions } from './Interfaces';
2
+ import type { ClientOptions, GraphQLResponse, JokeResult, JokeResultWithImage, JokeSearchResult, RequestOptions, SearchOptions, SlackJokeResult } from './Interfaces';
3
3
  export declare class ICanHazDadJokeAPI {
4
4
  protected readonly apiClient: APIClient;
5
5
  protected readonly options: ClientOptions;
6
6
  constructor(apiClient: APIClient, options: ClientOptions);
7
7
  /**
8
8
  * Fetch a dad joke by ID.
9
- * @see https://icanhazdadjoke.com/api#fetch-a-dad-joke
10
9
  */
11
10
  getById(id: string, options: {
12
11
  withImage: true;
@@ -14,7 +13,6 @@ export declare class ICanHazDadJokeAPI {
14
13
  getById(id: string, options?: RequestOptions): Promise<JokeResult>;
15
14
  /**
16
15
  * Fetch a dad joke by ID.
17
- * @see https://icanhazdadjoke.com/api#fetch-a-dad-joke
18
16
  */
19
17
  getByID(id: string, options: {
20
18
  withImage: true;
@@ -22,16 +20,22 @@ export declare class ICanHazDadJokeAPI {
22
20
  getByID(id: string, options?: RequestOptions): Promise<JokeResult>;
23
21
  /**
24
22
  * Fetch a random dad joke.
25
- * @see https://icanhazdadjoke.com/api#fetch-a-random-dad-joke
26
23
  */
27
24
  getRandom(options: {
28
25
  withImage: true;
29
26
  }): Promise<JokeResultWithImage>;
30
27
  getRandom(options?: RequestOptions): Promise<JokeResult>;
28
+ /**
29
+ * Fetch a random dad joke as a Slack message.
30
+ */
31
+ getSlack(): Promise<SlackJokeResult>;
32
+ /**
33
+ * Execute a GraphQL query.
34
+ */
35
+ graphql(query: string): Promise<GraphQLResponse>;
31
36
  /**
32
37
  * Search for dad jokes.
33
38
  * @param options Search options (default: list all jokes)
34
- * @see https://icanhazdadjoke.com/api#search-for-dad-jokes
35
39
  */
36
40
  search(options: SearchOptions): Promise<JokeSearchResult>;
37
41
  search(query: string, options?: Omit<SearchOptions, 'term'>): Promise<JokeSearchResult>;
@@ -10,6 +10,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.ICanHazDadJokeAPI = void 0;
13
+ const MIN_SEARCH_LIMIT = 1;
14
+ const MAX_SEARCH_LIMIT = 30;
13
15
  class ICanHazDadJokeAPI {
14
16
  constructor(apiClient, options) {
15
17
  this.apiClient = apiClient;
@@ -38,11 +40,32 @@ class ICanHazDadJokeAPI {
38
40
  return metaData;
39
41
  });
40
42
  }
43
+ /**
44
+ * Fetch a random dad joke as a Slack message.
45
+ */
46
+ getSlack() {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ const { data } = yield this.apiClient.get('/slack');
49
+ return data;
50
+ });
51
+ }
52
+ /**
53
+ * Execute a GraphQL query.
54
+ */
55
+ graphql(query) {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ const { data } = yield this.apiClient.post('/graphql', { query });
58
+ return data;
59
+ });
60
+ }
41
61
  search(query_1) {
42
62
  return __awaiter(this, arguments, void 0, function* (query, options = {}) {
43
63
  if (typeof query === 'string') {
44
64
  options.term = query;
45
65
  }
66
+ if (options.limit !== undefined && (options.limit < MIN_SEARCH_LIMIT || options.limit > MAX_SEARCH_LIMIT)) {
67
+ throw new Error(`The search limit must be between ${MIN_SEARCH_LIMIT} and ${MAX_SEARCH_LIMIT}, got ${options.limit}.`);
68
+ }
46
69
  const { data } = yield this.apiClient.get('/search', {
47
70
  params: options,
48
71
  });
@@ -1,6 +1,17 @@
1
1
  export interface ClientOptions {
2
2
  apiUrl?: string;
3
3
  }
4
+ export interface GraphQLData {
5
+ joke: GraphQLJokeResult;
6
+ }
7
+ export interface GraphQLJokeResult {
8
+ id: string;
9
+ joke: string;
10
+ permalink: string;
11
+ }
12
+ export interface GraphQLResponse {
13
+ data: GraphQLData;
14
+ }
4
15
  export interface JokeResult {
5
16
  id: string;
6
17
  joke: string;
@@ -31,3 +42,13 @@ export interface SearchOptions {
31
42
  /** search term to use (default: list all jokes) */
32
43
  term?: string;
33
44
  }
45
+ export interface SlackAttachment {
46
+ fallback: string;
47
+ footer: string;
48
+ text: string;
49
+ }
50
+ export interface SlackJokeResult {
51
+ attachments: SlackAttachment[];
52
+ response_type: string;
53
+ username: string;
54
+ }
package/package.json CHANGED
@@ -8,10 +8,12 @@
8
8
  "description": "An icanhazdadjoke.com API client with a CLI.",
9
9
  "devDependencies": {
10
10
  "@types/node": "~24",
11
+ "nock": "14.0.12",
11
12
  "rimraf": "6.1.3",
12
13
  "tsx": "4.21.0",
13
- "typedoc": "0.28.18",
14
- "typescript": "6.0.2"
14
+ "typedoc": "0.28.19",
15
+ "typescript": "6.0.3",
16
+ "vitest": "4.1.5"
15
17
  },
16
18
  "engines": {
17
19
  "node": ">= 21"
@@ -37,7 +39,7 @@
37
39
  "clean": "rimraf dist",
38
40
  "dist": "yarn clean && yarn build",
39
41
  "start": "tsx src/cli.ts",
40
- "test": "exit 0"
42
+ "test": "vitest"
41
43
  },
42
- "version": "2.4.7"
44
+ "version": "2.6.0"
43
45
  }