Skip to content

Commit

Permalink
throw error if featured snippet DA fieldtype is not multi line text O…
Browse files Browse the repository at this point in the history
…R rich text
  • Loading branch information
Yen Truong committed Aug 15, 2022
1 parent 852e91d commit fcd9dc8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/search-core.directanswer.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ export interface DirectAnswer<T = unknown>
| [fieldType](./search-core.directanswer.fieldtype.md) | [EnumOrLiteral](./search-core.enumorliteral.md)<!-- -->&lt;[BuiltInFieldType](./search-core.builtinfieldtype.md)<!-- -->&gt; \| 'unknown' | The field type of the direct answer. |
| [relatedResult](./search-core.directanswer.relatedresult.md) | [Result](./search-core.result.md) | The entity associated with the direct answer. |
| [type](./search-core.directanswer.type.md) | [DirectAnswerType](./search-core.directanswertype.md) | The [DirectAnswerType](./search-core.directanswertype.md)<!-- -->. |
| [value?](./search-core.directanswer.value.md) | T | <i>(Optional)</i> The result of the direct answer. |
| [value?](./search-core.directanswer.value.md) | T | <i>(Optional)</i> The value of the direct answer. |
| [verticalKey](./search-core.directanswer.verticalkey.md) | string | The vertical key of the direct answer. |

2 changes: 1 addition & 1 deletion docs/search-core.directanswer.value.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## DirectAnswer.value property

The result of the direct answer.
The value of the direct answer.

<b>Signature:</b>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface DirectAnswer<T = unknown> {
/** The {@link DirectAnswerType}. */
type: DirectAnswerType,
/**
* The result of the direct answer.
* The value of the direct answer.
*
* @remarks
* A value will not be present if the {@link DirectAnswer."type"} is 'FEATURED_SNIPPET'
Expand Down
6 changes: 5 additions & 1 deletion src/transformers/searchservice/createDirectAnswer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ export function createDirectAnswer(data: any): FeaturedSnippetDirectAnswer | Fie
fieldApiName: data.answer.fieldApiName,
};
} else if (isFeaturedSnippetDirectAnswer) {
const fieldType = commonDirectAnswerData.fieldType;
if (fieldType != BuiltInFieldType.MultiLineText && fieldType != BuiltInFieldType.RichText) {
throw new Error(`Unexpected fieldType for featured snippet direct answer: ${fieldType}`);
}
return {
type: DirectAnswerType.FeaturedSnippet,
...commonDirectAnswerData,
snippet: data.answer.snippet
};
} else {
throw new Error('The Search API returned an unknown direct answer type');
throw new Error(`The Search API returned an unknown direct answer type: ${data?.type}`);
}
}
20 changes: 20 additions & 0 deletions tests/transformers/searchservice/createDirectAnswer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ it('can create a FeaturedSnippetDirectAnswer', () => {
expect(actualDirectAnswer).toMatchObject(expectedDirectAnswer);
});

it('can handle unexpected fieldType for a FeaturedSnippetDirectAnswer', () => {
const apiFeaturedSnippetDirectAnswer = {
type: 'FEATURED_SNIPPET',
answer: {
value: 'Honolulu. Hawaii',
fieldType: 'phone',
snippet: {
matchedSubstrings: [{ offset: 31, length: 16 }],
value: '111 222 3333'
}
},
relatedItem: {
verticalConfigId: 'wiki_bios',
data: {}
}
};
expect(() => createDirectAnswer(apiFeaturedSnippetDirectAnswer))
.toThrow('Unexpected fieldType for featured snippet direct answer: phone');
});

it('can create a FieldValueDirectAnswer', () => {
const apiFieldValueDirectAnswer = {
type: 'FIELD_VALUE',
Expand Down

0 comments on commit fcd9dc8

Please sign in to comment.