Skip to content

Commit 41aa6e3

Browse files
feat: return raw phone number when requested via context (#15)
* feat: return raw phone number when requested via context
1 parent 44fa2c4 commit 41aa6e3

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,32 @@ Example response:
190190
}
191191
```
192192

193+
## Get the raw phone number value
194+
195+
If you need to access the raw phone number value you can use context when you query:
196+
197+
```ts
198+
const employees = await payload.find({
199+
collection: 'employees',
200+
where: {
201+
phoneNumber: {
202+
equals: '+4712345678'
203+
}
204+
},
205+
context: {
206+
phoneNumberPluginReturnRawValue: true,
207+
},
208+
})
209+
```
210+
211+
Example response:
212+
213+
```json
214+
{
215+
"phoneNumber": "+4712345678"
216+
}
217+
```
218+
193219
## Additional Information
194220

195221
A region code is an ISO 3166-1 alpha-2 code.

dev/int.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,30 @@ describe('Phone Number Plugin integration tests', () => {
133133
regionCode: 'US',
134134
});
135135
});
136+
137+
test('returns raw phone number string when requested via context', async () => {
138+
const phoneNumber = '+4745360001';
139+
140+
const employee = await payload.create({
141+
collection: 'employees',
142+
data: {
143+
name: 'Test',
144+
phoneNumber,
145+
},
146+
});
147+
148+
expect(employee.phoneNumber).toMatchObject({
149+
e164: phoneNumber,
150+
});
151+
152+
const employeeWithRawPhone = await payload.findByID({
153+
collection: 'employees',
154+
id: employee.id,
155+
context: {
156+
phoneNumberPluginReturnRawValue: true,
157+
},
158+
});
159+
160+
expect(employeeWithRawPhone.phoneNumber).toBe(phoneNumber);
161+
});
136162
});

src/fields/phoneNumber.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ const phoneNumberField = <T extends RegionCode[] | undefined = undefined>({ requ
100100
},
101101
],
102102
afterRead: [
103-
({ value }) => {
104-
if (value && typeof value === 'string') {
103+
({ value, context }) => {
104+
if (value && typeof value === 'string' && context['phoneNumberPluginReturnRawValue'] !== true) {
105105
return getPhoneNumberDetails(value);
106106
}
107107
return value;

0 commit comments

Comments
 (0)