@yaks/phone 0.0.0-stage → 0.2.36

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.
Files changed (81) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +251 -2
  3. package/_dist/call.d.ts +37 -0
  4. package/_dist/call.d.ts.map +1 -0
  5. package/_dist/effects.d.ts +5 -0
  6. package/_dist/effects.d.ts.map +1 -0
  7. package/_dist/mobile.d.ts +41 -0
  8. package/_dist/mobile.d.ts.map +1 -0
  9. package/_dist/mod.d.ts +27 -0
  10. package/_dist/mod.d.ts.map +1 -0
  11. package/_dist/numbers.d.ts +150 -0
  12. package/_dist/numbers.d.ts.map +1 -0
  13. package/_dist/options.d.ts +21 -0
  14. package/_dist/options.d.ts.map +1 -0
  15. package/_dist/plugin.d.ts +54 -0
  16. package/_dist/plugin.d.ts.map +1 -0
  17. package/_dist/provider.d.ts +72 -0
  18. package/_dist/provider.d.ts.map +1 -0
  19. package/_dist/received.d.ts +24 -0
  20. package/_dist/received.d.ts.map +1 -0
  21. package/_dist/replies.d.ts +18 -0
  22. package/_dist/replies.d.ts.map +1 -0
  23. package/_dist/send.d.ts +16 -0
  24. package/_dist/send.d.ts.map +1 -0
  25. package/_dist/stash.d.ts +19 -0
  26. package/_dist/stash.d.ts.map +1 -0
  27. package/_dist/text.d.ts +102 -0
  28. package/_dist/text.d.ts.map +1 -0
  29. package/_dist/tools.d.ts +3 -0
  30. package/_dist/tools.d.ts.map +1 -0
  31. package/_dist/vocab.d.ts +8 -0
  32. package/_dist/vocab.d.ts.map +1 -0
  33. package/call.js +5 -0
  34. package/call.js.map +1 -0
  35. package/call.ts +53 -0
  36. package/deno.json +1 -0
  37. package/effects.js +30 -0
  38. package/effects.js.map +1 -0
  39. package/effects.ts +35 -0
  40. package/mobile.js +75 -0
  41. package/mobile.js.map +1 -0
  42. package/mobile.ts +72 -0
  43. package/mod.js +28 -0
  44. package/mod.js.map +1 -0
  45. package/mod.ts +78 -0
  46. package/numbers.js +72 -0
  47. package/numbers.js.map +1 -0
  48. package/numbers.ts +259 -0
  49. package/options.js +3 -0
  50. package/options.js.map +1 -0
  51. package/options.ts +23 -0
  52. package/package.json +44 -4
  53. package/plugin.js +155 -0
  54. package/plugin.js.map +1 -0
  55. package/plugin.ts +226 -0
  56. package/provider.js +48 -0
  57. package/provider.js.map +1 -0
  58. package/provider.ts +119 -0
  59. package/received.js +72 -0
  60. package/received.js.map +1 -0
  61. package/received.ts +76 -0
  62. package/recipients.js +58 -0
  63. package/replies.js +23 -0
  64. package/replies.js.map +1 -0
  65. package/replies.ts +38 -0
  66. package/send.js +136 -0
  67. package/send.js.map +1 -0
  68. package/send.ts +131 -0
  69. package/stash.js +26 -0
  70. package/stash.js.map +1 -0
  71. package/stash.ts +41 -0
  72. package/text.js +127 -0
  73. package/text.js.map +1 -0
  74. package/text.ts +192 -0
  75. package/tools.js +62 -0
  76. package/tools.js.map +1 -0
  77. package/tools.ts +55 -0
  78. package/vocab.js +18 -0
  79. package/vocab.js.map +1 -0
  80. package/vocab.json +245 -0
  81. package/vocab.ts +19 -0
package/effects.js ADDED
@@ -0,0 +1,30 @@
1
+ // The texter is lent at the effect boundary, selected by sender.via: by the
2
+ // host, or by a provider plugin beside this one (./plugin.ts).
3
+ import { sending } from "./send.js";
4
+ import { stash } from "./stash.js";
5
+ export let effects = (host, options = {})=>{
6
+ let kept;
7
+ return {
8
+ text_post: async (event, tx, write, attempt)=>{
9
+ let via = options.sender?.via;
10
+ let texter = via == 'stash' ? kept ??= stash() : via ? host?.texters?.[via] : undefined;
11
+ let reason = !via ? 'waiting for a configured sender' : !texter ? `waiting for a texter: nothing lends ${via}` : texter.waiting?.();
12
+ if (!reason) {
13
+ return sending({
14
+ sender: texter
15
+ })(event, tx, write, attempt);
16
+ }
17
+ await write([
18
+ {
19
+ entity: event.entity,
20
+ deliver: {
21
+ waiting: reason
22
+ }
23
+ }
24
+ ]);
25
+ if (attempt?.defer) return attempt.defer(60_000);
26
+ throw new Error(`@yaks/phone — ${reason}`);
27
+ }
28
+ };
29
+ };
30
+ //# sourceMappingURL=effects.js.map
package/effects.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"effects.js","sources":["./effects.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,+DAA+D;AAI/D,SAAS,OAAO,oBAAmB;AACnC,SAAqB,KAAK,qBAAoB;AAE9C,OAAO,IAAI,UAAU,CACnB,MACA,UAAmB,CAAC,CAAC;EAErB,IAAI;EACJ,OAAO;IACL,WAAW,OAAO,OAAO,IAAI,OAAO;MAClC,IAAI,MAAM,QAAQ,MAAM,EAAE;MAC1B,IAAI,SAAS,OAAO,UAChB,SAAS,UACT,MACA,MAAM,SAAS,CAAC,IAAI,GACpB;MACJ,IAAI,SAAS,CAAC,MACV,oCACA,CAAC,SACD,CAAC,oCAAoC,EAAE,KAAK,GAC5C,OAAO,OAAO;MAClB,IAAI,CAAC,QAAQ;QACX,OAAO,QAAQ;UAAE,QAAQ;QAAQ,GAAG,OAAO,IAAI,OAAO;MACxD;MACA,MAAM,MAAM;QAAC;UAAE,QAAQ,MAAM,MAAM;UAAE,SAAS;YAAE,SAAS;UAAO;QAAE;OAAE;MACpE,IAAI,SAAS,OAAO,OAAO,QAAQ,KAAK,CAAC;MACzC,MAAM,IAAI,MAAM,CAAC,cAAc,EAAE,QAAQ;IAC3C;EACF;AACF,EAAC"}
package/effects.ts ADDED
@@ -0,0 +1,35 @@
1
+ // The texter is lent at the effect boundary, selected by sender.via: by the
2
+ // host, or by a provider plugin beside this one (./plugin.ts).
3
+ import type { Handlers } from 'jsr:@yaks/effects@0.2.36'
4
+ import type { Host } from 'jsr:@yaks/host@0.2.36'
5
+ import type { Options } from './options.ts'
6
+ import { sending } from './send.js'
7
+ import { type Stash, stash } from './stash.js'
8
+
9
+ export let effects = (
10
+ host?: Pick<Host, 'texters'> | null,
11
+ options: Options = {},
12
+ ): Handlers => {
13
+ let kept: Stash | undefined
14
+ return {
15
+ text_post: async (event, tx, write, attempt) => {
16
+ let via = options.sender?.via
17
+ let texter = via == 'stash'
18
+ ? kept ??= stash()
19
+ : via
20
+ ? host?.texters?.[via]
21
+ : undefined
22
+ let reason = !via
23
+ ? 'waiting for a configured sender'
24
+ : !texter
25
+ ? `waiting for a texter: nothing lends ${via}`
26
+ : texter.waiting?.()
27
+ if (!reason) {
28
+ return sending({ sender: texter! })(event, tx, write, attempt)
29
+ }
30
+ await write([{ entity: event.entity, deliver: { waiting: reason } }])
31
+ if (attempt?.defer) return attempt.defer(60_000)
32
+ throw new Error(`@yaks/phone — ${reason}`)
33
+ },
34
+ }
35
+ }
package/mobile.js ADDED
@@ -0,0 +1,75 @@
1
+ // A typed number in the form `phone{number}` keeps, checked against
2
+ // libphonenumber's mobile metadata: a module of its own, so what imports the
3
+ // rest of @yaks/phone (a sender's statuses, a received text) never loads it.
4
+ import { parsePhoneNumberFromString } from "libphonenumber-js/mobile";
5
+ /** A directory number in E.164 form, including landlines and other countries.
6
+ * Unqualified numbers are interpreted in the US. */ /// e164('(269) 555-0142') -> '+12695550142'
7
+ /// e164('+44 20 7946 0018') -> '+442079460018'
8
+ /// e164('555-0142') -> null
9
+ export let e164 = (typed)=>{
10
+ let number = parsePhoneNumberFromString(typed, 'US');
11
+ return number?.isPossible() ? number.number : null;
12
+ };
13
+ /**
14
+ * A US mobile number in E.164 form, or null when `typed` is not one.
15
+ *
16
+ * ```ts
17
+ * import { equal } from '@yaks/testing'
18
+ * import { mobile } from '@yaks/phone/mobile'
19
+ * equal(mobile('(269) 555-0142'), '+12695550142')
20
+ * equal(mobile('+1 269 555 0142'), '+12695550142')
21
+ * equal(mobile('555-0142'), null)
22
+ * equal(mobile('(069) 555-0142'), null)
23
+ * equal(mobile('(876) 555-0142'), null)
24
+ * equal(mobile('(900) 555-0142'), null)
25
+ * equal(mobile('(530) 555-0142'), '+15305550142')
26
+ * equal(mobile('(851) 555-0142'), null)
27
+ * equal(mobile('(299) 555-0142'), null)
28
+ * equal(mobile('(800) 555-0142'), null)
29
+ * equal(mobile('(416) 555-0142'), null)
30
+ * equal(mobile('(787) 555-0142'), '+17875550142')
31
+ * equal(mobile('+44 7700 900123'), null)
32
+ * ```
33
+ */ export let mobile = (typed)=>{
34
+ let number = parsePhoneNumberFromString(typed, 'US');
35
+ return number?.isValid() && US.has(number.country) ? number.number : null;
36
+ };
37
+ // The regions that are a place in the US: the states and the territories.
38
+ // Everything else sharing +1 is refused: Canada, the Caribbean and Atlantic
39
+ // countries, and through libphonenumber's mobile metadata toll-free,
40
+ // premium-rate 900, the other non-geographic codes, N11 and codes not yet
41
+ // assigned. A text to the Caribbean or to 900 costs many times a US text and
42
+ // is what SMS pumping walks.
43
+ let US = new Set([
44
+ 'US',
45
+ 'PR',
46
+ 'VI',
47
+ 'GU',
48
+ 'AS',
49
+ 'MP'
50
+ ]);
51
+ /**
52
+ * Where a number is from, as its numbering plan says: its country, and its
53
+ * area code where the plan has them (+1, the North American plan). Nothing
54
+ * for a number with no place, a toll-free one say.
55
+ *
56
+ * ```ts
57
+ * import { equal } from '@yaks/testing'
58
+ * import { dialing } from '@yaks/phone/mobile'
59
+ * equal(dialing('+12695550142'), { country: 'US', area: '269' })
60
+ * equal(dialing('+14165550142'), { country: 'CA', area: '416' })
61
+ * equal(dialing('+18005550142'), {})
62
+ * equal(dialing('+33612345678'), { country: 'FR' })
63
+ * ```
64
+ */ export let dialing = (number)=>{
65
+ let parsed = parsePhoneNumberFromString(number);
66
+ let country = parsed?.country;
67
+ if (!parsed || !country) return {};
68
+ return parsed.countryCallingCode == '1' ? {
69
+ country,
70
+ area: parsed.nationalNumber.slice(0, 3)
71
+ } : {
72
+ country
73
+ };
74
+ };
75
+ //# sourceMappingURL=mobile.js.map
package/mobile.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mobile.js","sources":["./mobile.ts"],"names":[],"mappings":"AAAA,oEAAoE;AACpE,6EAA6E;AAC7E,6EAA6E;AAE7E,SAAS,0BAA0B,mCAA+C;AAElF;kDACkD,GAClD,4CAA4C;AAC5C,+CAA+C;AAC/C,4BAA4B;AAC5B,OAAO,IAAI,OAAO,CAAC;EACjB,IAAI,SAAS,2BAA2B,OAAO;EAC/C,OAAO,QAAQ,eAAe,OAAO,MAAM,GAAG;AAChD,EAAC;AAED;;;;;;;;;;;;;;;;;;;;CAoBC,GACD,OAAO,IAAI,SAAS,CAAC;EACnB,IAAI,SAAS,2BAA2B,OAAO;EAC/C,OAAO,QAAQ,aAAa,GAAG,GAAG,CAAC,OAAO,OAAO,IAAI,OAAO,MAAM,GAAG;AACvE,EAAC;AAED,0EAA0E;AAC1E,4EAA4E;AAC5E,qEAAqE;AACrE,0EAA0E;AAC1E,6EAA6E;AAC7E,6BAA6B;AAC7B,IAAI,KAAK,IAAI,IAAwB;EAAC;EAAM;EAAM;EAAM;EAAM;EAAM;CAAK;AAEzE;;;;;;;;;;;;;CAaC,GACD,OAAO,IAAI,UAAU,CAAC;EACpB,IAAI,SAAS,2BAA2B;EACxC,IAAI,UAAU,QAAQ;EACtB,IAAI,CAAC,UAAU,CAAC,SAAS,OAAO,CAAC;EACjC,OAAO,OAAO,kBAAkB,IAAI,MAChC;IAAE;IAAS,MAAM,OAAO,cAAc,CAAC,KAAK,CAAC,GAAG;EAAG,IACnD;IAAE;EAAQ;AAChB,EAAC"}
package/mobile.ts ADDED
@@ -0,0 +1,72 @@
1
+ // A typed number in the form `phone{number}` keeps, checked against
2
+ // libphonenumber's mobile metadata: a module of its own, so what imports the
3
+ // rest of @yaks/phone (a sender's statuses, a received text) never loads it.
4
+
5
+ import { parsePhoneNumberFromString } from 'libphonenumber-js/mobile'
6
+
7
+ /** A directory number in E.164 form, including landlines and other countries.
8
+ * Unqualified numbers are interpreted in the US. */
9
+ /// e164('(269) 555-0142') -> '+12695550142'
10
+ /// e164('+44 20 7946 0018') -> '+442079460018'
11
+ /// e164('555-0142') -> null
12
+ export let e164 = (typed: string): string | null => {
13
+ let number = parsePhoneNumberFromString(typed, 'US')
14
+ return number?.isPossible() ? number.number : null
15
+ }
16
+
17
+ /**
18
+ * A US mobile number in E.164 form, or null when `typed` is not one.
19
+ *
20
+ * ```ts
21
+ * import { equal } from '@yaks/testing'
22
+ * import { mobile } from '@yaks/phone/mobile'
23
+ * equal(mobile('(269) 555-0142'), '+12695550142')
24
+ * equal(mobile('+1 269 555 0142'), '+12695550142')
25
+ * equal(mobile('555-0142'), null)
26
+ * equal(mobile('(069) 555-0142'), null)
27
+ * equal(mobile('(876) 555-0142'), null)
28
+ * equal(mobile('(900) 555-0142'), null)
29
+ * equal(mobile('(530) 555-0142'), '+15305550142')
30
+ * equal(mobile('(851) 555-0142'), null)
31
+ * equal(mobile('(299) 555-0142'), null)
32
+ * equal(mobile('(800) 555-0142'), null)
33
+ * equal(mobile('(416) 555-0142'), null)
34
+ * equal(mobile('(787) 555-0142'), '+17875550142')
35
+ * equal(mobile('+44 7700 900123'), null)
36
+ * ```
37
+ */
38
+ export let mobile = (typed: string): string | null => {
39
+ let number = parsePhoneNumberFromString(typed, 'US')
40
+ return number?.isValid() && US.has(number.country) ? number.number : null
41
+ }
42
+
43
+ // The regions that are a place in the US: the states and the territories.
44
+ // Everything else sharing +1 is refused: Canada, the Caribbean and Atlantic
45
+ // countries, and through libphonenumber's mobile metadata toll-free,
46
+ // premium-rate 900, the other non-geographic codes, N11 and codes not yet
47
+ // assigned. A text to the Caribbean or to 900 costs many times a US text and
48
+ // is what SMS pumping walks.
49
+ let US = new Set<string | undefined>(['US', 'PR', 'VI', 'GU', 'AS', 'MP'])
50
+
51
+ /**
52
+ * Where a number is from, as its numbering plan says: its country, and its
53
+ * area code where the plan has them (+1, the North American plan). Nothing
54
+ * for a number with no place, a toll-free one say.
55
+ *
56
+ * ```ts
57
+ * import { equal } from '@yaks/testing'
58
+ * import { dialing } from '@yaks/phone/mobile'
59
+ * equal(dialing('+12695550142'), { country: 'US', area: '269' })
60
+ * equal(dialing('+14165550142'), { country: 'CA', area: '416' })
61
+ * equal(dialing('+18005550142'), {})
62
+ * equal(dialing('+33612345678'), { country: 'FR' })
63
+ * ```
64
+ */
65
+ export let dialing = (number: string): { country?: string; area?: string } => {
66
+ let parsed = parsePhoneNumberFromString(number)
67
+ let country = parsed?.country
68
+ if (!parsed || !country) return {}
69
+ return parsed.countryCallingCode == '1'
70
+ ? { country, area: parsed.nationalNumber.slice(0, 3) }
71
+ : { country }
72
+ }
package/mod.js ADDED
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Phone numbers and texts: a person's number as `phone{number}` and the
3
+ * consent they gave to be texted as `sms_consent{person, consent, at}`.
4
+ * `mobile` (@yaks/phone/mobile) puts a typed number in the form both are kept
5
+ * in.
6
+ */ export { phoneDoc } from "./vocab.js";
7
+ /**
8
+ * A number in `mobile`'s form as a person in the US writes it; anything else
9
+ * as it came.
10
+ *
11
+ * ```ts
12
+ * import { equal } from '@yaks/testing'
13
+ * import { shown } from '@yaks/phone'
14
+ * equal(shown('+12695550142'), '(269) 555-0142')
15
+ * equal(shown('+447700900123'), '+447700900123')
16
+ * ```
17
+ */ export let shown = (number)=>{
18
+ let d = /^\+1(\d{3})(\d{3})(\d{4})$/.exec(number);
19
+ return d ? `(${d[1]}) ${d[2]}-${d[3]}` : number;
20
+ };
21
+ export { Blocked, consentEid, consentRemoved, Denied, echo, keyword, othersIn, stoppedBy, thread } from "./text.js";
22
+ export { replies } from "./replies.js";
23
+ export { stash } from "./stash.js";
24
+ export { arrivalEid, kept, settled } from "./received.js";
25
+ export { owed, sending } from "./send.js";
26
+ export { listens, sender, verified } from "./provider.js";
27
+ export { CallStopError, shelf } from "./numbers.js";
28
+ //# sourceMappingURL=mod.js.map
package/mod.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA;;;;;CAKC,GAED,SAAS,QAAQ,qBAAoB;AAErC;;;;;;;;;;CAUC,GACD,OAAO,IAAI,QAAQ,CAAC;EAClB,IAAI,IAAI,6BAA6B,IAAI,CAAC;EAC1C,OAAO,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,GAAG;AAC3C,EAAC;AAED,SAEE,OAAO,EACP,UAAU,EACV,cAAc,EACd,MAAM,EACN,IAAI,EAEJ,OAAO,EACP,QAAQ,EAIR,SAAS,EAET,MAAM,oBACU;AAClB,SAAuB,OAAO,uBAAsB;AACpD,SAAqB,KAAK,qBAAoB;AAC9C,SAAS,UAAU,EAAE,IAAI,EAAE,OAAO,wBAAuB;AACzD,SAAS,IAAI,EAAE,OAAO,oBAAiC;AAGvD,SAKE,OAAO,EAGP,MAAM,EACN,QAAQ,wBACY;AAEtB,SAIE,aAAa,EAOb,KAAK,uBAMc"}
package/mod.ts ADDED
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Phone numbers and texts: a person's number as `phone{number}` and the
3
+ * consent they gave to be texted as `sms_consent{person, consent, at}`.
4
+ * `mobile` (@yaks/phone/mobile) puts a typed number in the form both are kept
5
+ * in.
6
+ */
7
+
8
+ export { phoneDoc } from './vocab.js'
9
+
10
+ /**
11
+ * A number in `mobile`'s form as a person in the US writes it; anything else
12
+ * as it came.
13
+ *
14
+ * ```ts
15
+ * import { equal } from '@yaks/testing'
16
+ * import { shown } from '@yaks/phone'
17
+ * equal(shown('+12695550142'), '(269) 555-0142')
18
+ * equal(shown('+447700900123'), '+447700900123')
19
+ * ```
20
+ */
21
+ export let shown = (number: string): string => {
22
+ let d = /^\+1(\d{3})(\d{3})(\d{4})$/.exec(number)
23
+ return d ? `(${d[1]}) ${d[2]}-${d[3]}` : number
24
+ }
25
+
26
+ export {
27
+ type Arrival,
28
+ Blocked,
29
+ consentEid,
30
+ consentRemoved,
31
+ Denied,
32
+ echo,
33
+ type Keyword,
34
+ keyword,
35
+ othersIn,
36
+ type Outcome,
37
+ type Receipt,
38
+ type Sender,
39
+ stoppedBy,
40
+ type Text,
41
+ thread,
42
+ } from './text.js'
43
+ export { type Replies, replies } from './replies.js'
44
+ export { type Stash, stash } from './stash.js'
45
+ export { arrivalEid, kept, settled } from './received.js'
46
+ export { owed, sending, type Texting } from './send.js'
47
+ export type { Options, Transport } from './options.ts'
48
+
49
+ export {
50
+ type Account,
51
+ type Attempt,
52
+ type Heard,
53
+ type Held,
54
+ listens,
55
+ type Own,
56
+ type Provider,
57
+ sender,
58
+ verified,
59
+ } from './provider.js'
60
+ export type { Caller, Frame, Line, Ring } from './call.ts'
61
+ export {
62
+ type Address,
63
+ type Bought,
64
+ type Call,
65
+ CallStopError,
66
+ type Entity,
67
+ type Login,
68
+ type Numbers,
69
+ type Offer,
70
+ type Search,
71
+ type Shelf,
72
+ shelf,
73
+ type SubmitLogin,
74
+ type TollFree,
75
+ type Verification,
76
+ type Voice,
77
+ type Ways,
78
+ } from './numbers.js'
package/numbers.js ADDED
@@ -0,0 +1,72 @@
1
+ // A provider's numbers: what it has on offer and at what price, buying one
2
+ // onto the account a phone texts through, giving one back, and the
3
+ // toll-free verification carriers ask of a number before it texts. A
4
+ // provider gives its `Numbers` as data (./provider.ts `numbers`); nothing
5
+ // here names one.
6
+ /** What to look for. */ /** The directions a provider confirmed stopped before the other work failed. */ export class CallStopError extends AggregateError {
7
+ stopped;
8
+ constructor(stopped, errors){
9
+ super(errors, 'The phone company could not shut all calls'), this.stopped = stopped;
10
+ }
11
+ }
12
+ /**
13
+ * Numbers in memory, for tests and a box with no provider: `offers` are on
14
+ * offer until bought, a bought number is held, and a verification is pending
15
+ * until a test sets its status in `filed`.
16
+ *
17
+ * ```ts
18
+ * import { equal } from '@yaks/testing'
19
+ * import { shelf } from '@yaks/phone'
20
+ * let numbers = shelf([{ number: '+18885550100', upfront: 1, monthly: 1,
21
+ * currency: 'USD', sms: true, voice: true }])
22
+ * equal((await numbers.find({ tollFree: true })).length, 1)
23
+ * equal((await numbers.buy('+18885550100')).status, 'held')
24
+ * equal(await numbers.find({ tollFree: true }), [])
25
+ * ```
26
+ */ export let shelf = (offers = [])=>{
27
+ let held = new Set();
28
+ let filed = new Map();
29
+ let free = (n)=>/^\+18(00|33|44|55|66|77|88)/.test(n);
30
+ let state = (id)=>{
31
+ let form = filed.get(id);
32
+ if (!form) throw new Error(`No verification ${id}`);
33
+ return {
34
+ id,
35
+ number: form.number,
36
+ status: form.status,
37
+ said: form.status
38
+ };
39
+ };
40
+ return {
41
+ held,
42
+ filed,
43
+ find: (s)=>Promise.resolve(offers.filter((o)=>!held.has(o.number) && (!s.tollFree || free(o.number)) && (!s.number || o.number == s.number) && (!s.area || o.number.startsWith(`+1${s.area}`)) && (!s.region || !!o.place?.endsWith(`, ${s.region}`)) && (!s.contains || o.number.includes(s.contains))).slice(0, s.limit ?? 5)),
44
+ buy: (number)=>{
45
+ if (held.has(number) || !offers.some((o)=>o.number == number)) {
46
+ return Promise.reject(new Error(`${number} is not on offer`));
47
+ }
48
+ held.add(number);
49
+ return Promise.resolve({
50
+ number,
51
+ order: `order-${held.size}`,
52
+ status: 'held'
53
+ });
54
+ },
55
+ release: (number)=>{
56
+ if (!held.delete(number)) {
57
+ return Promise.reject(new Error(`${number} is not held`));
58
+ }
59
+ return Promise.resolve();
60
+ },
61
+ verify: (form)=>{
62
+ let id = `verification-${filed.size + 1}`;
63
+ filed.set(id, {
64
+ ...form,
65
+ status: 'pending'
66
+ });
67
+ return Promise.resolve(state(id));
68
+ },
69
+ verification: (id)=>Promise.resolve().then(()=>state(id))
70
+ };
71
+ };
72
+ //# sourceMappingURL=numbers.js.map
package/numbers.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"numbers.js","sources":["./numbers.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,mEAAmE;AACnE,qEAAqE;AACrE,0EAA0E;AAC1E,kBAAkB;AAElB,sBAAsB,GAkItB,8EAA8E,GAC9E,OAAO,MAAM,sBAAsB;;EACjC,YAAY,AAAS,OAAa,EAAE,MAAiB,CAAE;IACrD,KAAK,CAAC,QAAQ,oDADK,UAAA;EAErB;AACF;AAwDA;;;;;;;;;;;;;;CAcC,GACD,OAAO,IAAI,QAAQ,CAAC,SAAkB,EAAE;EACtC,IAAI,OAAO,IAAI;EACf,IAAI,QAAwB,IAAI;EAChC,IAAI,OAAO,CAAC,IAAc,8BAA8B,IAAI,CAAC;EAC7D,IAAI,QAAQ,CAAC;IACX,IAAI,OAAO,MAAM,GAAG,CAAC;IACrB,IAAI,CAAC,MAAM,MAAM,IAAI,MAAM,CAAC,gBAAgB,EAAE,IAAI;IAClD,OAAO;MAAE;MAAI,QAAQ,KAAK,MAAM;MAAE,QAAQ,KAAK,MAAM;MAAE,MAAM,KAAK,MAAM;IAAC;EAC3E;EACA,OAAO;IACL;IACA;IACA,MAAM,CAAC,IACL,QAAQ,OAAO,CACb,OAAO,MAAM,CAAC,CAAC,IACb,CAAC,KAAK,GAAG,CAAC,EAAE,MAAM,KAAK,CAAC,CAAC,EAAE,QAAQ,IAAI,KAAK,EAAE,MAAM,CAAC,KACrD,CAAC,CAAC,EAAE,MAAM,IAAI,EAAE,MAAM,IAAI,EAAE,MAAM,KAClC,CAAC,CAAC,EAAE,IAAI,IAAI,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC,KAC9C,CAAC,CAAC,EAAE,MAAM,IAAI,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC,KAClD,CAAC,CAAC,EAAE,QAAQ,IAAI,EAAE,MAAM,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC,GAC7C,KAAK,CAAC,GAAG,EAAE,KAAK,IAAI;IAE1B,KAAK,CAAC;MACJ,IAAI,KAAK,GAAG,CAAC,WAAW,CAAC,OAAO,IAAI,CAAC,CAAC,IAAM,EAAE,MAAM,IAAI,SAAS;QAC/D,OAAO,QAAQ,MAAM,CAAC,IAAI,MAAM,GAAG,OAAO,gBAAgB,CAAC;MAC7D;MACA,KAAK,GAAG,CAAC;MACT,OAAO,QAAQ,OAAO,CAAC;QACrB;QACA,OAAO,CAAC,MAAM,EAAE,KAAK,IAAI,EAAE;QAC3B,QAAQ;MACV;IACF;IACA,SAAS,CAAC;MACR,IAAI,CAAC,KAAK,MAAM,CAAC,SAAS;QACxB,OAAO,QAAQ,MAAM,CAAC,IAAI,MAAM,GAAG,OAAO,YAAY,CAAC;MACzD;MACA,OAAO,QAAQ,OAAO;IACxB;IACA,QAAQ,CAAC;MACP,IAAI,KAAK,CAAC,aAAa,EAAE,MAAM,IAAI,GAAG,GAAG;MACzC,MAAM,GAAG,CAAC,IAAI;QAAE,GAAG,IAAI;QAAE,QAAQ;MAAU;MAC3C,OAAO,QAAQ,OAAO,CAAC,MAAM;IAC/B;IACA,cAAc,CAAC,KAAO,QAAQ,OAAO,GAAG,IAAI,CAAC,IAAM,MAAM;EAC3D;AACF,EAAC"}
package/numbers.ts ADDED
@@ -0,0 +1,259 @@
1
+ // A provider's numbers: what it has on offer and at what price, buying one
2
+ // onto the account a phone texts through, giving one back, and the
3
+ // toll-free verification carriers ask of a number before it texts. A
4
+ // provider gives its `Numbers` as data (./provider.ts `numbers`); nothing
5
+ // here names one.
6
+
7
+ /** What to look for. */
8
+ export type Search = {
9
+ /** ISO country code (default US) */
10
+ country?: string
11
+ /** toll-free numbers only */
12
+ tollFree?: boolean
13
+ /** a three-digit area code */
14
+ area?: string
15
+ /** a two-letter state or province */
16
+ region?: string
17
+ /** a city or town, in `region` */
18
+ city?: string
19
+ /** digits the number contains */
20
+ contains?: string
21
+ /** exactly this number, in E.164 form, to quote it before buying */
22
+ number?: string
23
+ /** at most this many (default 5) */
24
+ limit?: number
25
+ }
26
+
27
+ /** A number on offer, at its price in `currency`. */
28
+ export type Offer = {
29
+ number: string
30
+ /** what buying it costs once */
31
+ upfront: number
32
+ /** what holding it costs each month */
33
+ monthly: number
34
+ currency: string
35
+ sms: boolean
36
+ voice: boolean
37
+ /** where the number is local to, in the provider's words, such as
38
+ * "BATTLE CRK, MI" */
39
+ place?: string
40
+ }
41
+
42
+ /** A call a number took or made, as its provider billed it: when it began,
43
+ * how long it took, and the dollars it cost. */
44
+ export type Call = { id: string; at: string; seconds: number; cost: number }
45
+
46
+ /** A softphone's sign-in to a number's calls: the SIP server, user and
47
+ * password. */
48
+ export type Login = { server: string; user: string; password: string }
49
+
50
+ /** A number bought: the provider's order for it, and whether it is held yet. */
51
+ export type Bought = {
52
+ number: string
53
+ order: string
54
+ /** `pending` until the provider has it on the account */
55
+ status: 'held' | 'pending'
56
+ }
57
+
58
+ /** Where an organization is. */
59
+ export type Address = {
60
+ street: string
61
+ street2?: string
62
+ city: string
63
+ /** a two-letter state or province */
64
+ state: string
65
+ zip: string
66
+ }
67
+
68
+ /** The kinds of organization carriers verify. */
69
+ export type Entity =
70
+ | 'sole_proprietor'
71
+ | 'private_profit'
72
+ | 'public_profit'
73
+ | 'non_profit'
74
+ | 'government'
75
+
76
+ /**
77
+ * What a toll-free verification asks: who sends, who answers for it, and the
78
+ * texts it sends and how people asked for them. The fields are the carriers'
79
+ * own, whichever provider files them.
80
+ */
81
+ export type TollFree = {
82
+ /** the toll-free number it verifies, in E.164 form */
83
+ number: string
84
+ business: {
85
+ /** the legal name */
86
+ name: string
87
+ /** the name people know it by, where it differs */
88
+ dba?: string
89
+ website: string
90
+ address: Address
91
+ entity: Entity
92
+ /** its tax or registration number: an EIN in the US */
93
+ registration?: { number: string; type: string; country: string }
94
+ }
95
+ contact: { first: string; last: string; email: string; phone: string }
96
+ /** the provider's name for what the texts are for, such as "Religious
97
+ * Services" */
98
+ useCase: string
99
+ /** what the texts are, in a sentence or two */
100
+ summary: string
101
+ /** a text as it will be sent */
102
+ sample: string
103
+ /** how many a month, in the provider's bands, such as "1,000" */
104
+ volume: string
105
+ /** how people ask to be texted, in words */
106
+ optIn: string
107
+ /** pictures of where they ask */
108
+ optInImages: string[]
109
+ /** the text sent when someone opts in, and the answer to HELP */
110
+ confirmation?: string
111
+ help?: string
112
+ /** the words that opt someone in, such as "START, YES" */
113
+ keywords?: string
114
+ privacy?: string
115
+ terms?: string
116
+ /** anything else the reviewer should know */
117
+ more?: string
118
+ /** the platform filing for its customer, where one does */
119
+ reseller?: string
120
+ }
121
+
122
+ /** Where a toll-free verification stands: `pending` while carriers review
123
+ * it, `needed` while they wait on an answer from the sender, `verified`, or
124
+ * `rejected` with why. `said` is the provider's own word for it. */
125
+ export type Verification = {
126
+ id: string
127
+ number: string
128
+ status: 'pending' | 'needed' | 'verified' | 'rejected'
129
+ said: string
130
+ reason?: string
131
+ }
132
+
133
+ /** Which ways a held number's calls go: `in`, calls to it ring it, and
134
+ * `out`, calls go out as it. */
135
+ export type Ways = { in: boolean; out: boolean }
136
+
137
+ /** The directions a provider confirmed stopped before the other work failed. */
138
+ export class CallStopError extends AggregateError {
139
+ constructor(readonly stopped: Ways, errors: unknown[]) {
140
+ super(errors, 'The phone company could not shut all calls')
141
+ }
142
+ }
143
+
144
+ /** Which seats may hold a softphone sign-in, and which one inherits the
145
+ * original account. A revoked seat loses its old sign-in even if it is
146
+ * promoted again. */
147
+ export type Voice = Ways & {
148
+ seats: string[]
149
+ requested?: string[]
150
+ legacy: string | null
151
+ revoked?: string[]
152
+ }
153
+
154
+ /** Claim one submission for a seat whose account the provider has yet to show. */
155
+ export type SubmitLogin = (seat: string) => Promise<boolean>
156
+
157
+ /** A provider's numbers, on the account its phone texts through. */
158
+ export type Numbers = {
159
+ find: (search: Search) => Promise<Offer[]>
160
+ /** buys a number on offer and puts its texts on the phone's profile;
161
+ * buying one already bought onto the account for this use completes */
162
+ buy: (number: string) => Promise<Bought>
163
+ /** Checks an order whose submission was uncertain, without placing another. */
164
+ ordered?: (number: string) => Promise<boolean>
165
+ /** for a provider that sends each number's texts to a URL of its own,
166
+ * sends a held number's texts to `callback` */
167
+ point?: (number: string, callback: string) => Promise<void>
168
+ /** gives a held number back; it costs nothing from then on */
169
+ release: (number: string) => Promise<void>
170
+ /** Removes provider resources left behind after a confirmed return. */
171
+ cleanup?: (number: string) => Promise<void>
172
+ /** files a toll-free verification */
173
+ verify: (form: TollFree) => Promise<Verification>
174
+ /** where a filed verification stands now */
175
+ verification: (id: string) => Promise<Verification>
176
+ /** for a provider whose numbers ring softphones, the owner's sign-in */
177
+ login?: (
178
+ number: string,
179
+ seat: string,
180
+ voice: Voice,
181
+ submit?: SubmitLogin,
182
+ ) => Promise<Login>
183
+ /** applies the call and seat policy to the number's existing sign-ins */
184
+ allow?: (number: string, voice: Voice, submit?: SubmitLogin) => Promise<void>
185
+ /** for a provider that bills each call, the calls a number took or made
186
+ * after `since`, oldest first */
187
+ calls?: (number: string, since: string) => Promise<Call[]>
188
+ }
189
+
190
+ /** A provider's {@link Numbers} kept in memory. */
191
+ export type Shelf = Numbers & {
192
+ /** the numbers held, by number */
193
+ held: Set<string>
194
+ /** every verification filed, by id */
195
+ filed: Map<string, TollFree & { status: Verification['status'] }>
196
+ }
197
+
198
+ /**
199
+ * Numbers in memory, for tests and a box with no provider: `offers` are on
200
+ * offer until bought, a bought number is held, and a verification is pending
201
+ * until a test sets its status in `filed`.
202
+ *
203
+ * ```ts
204
+ * import { equal } from '@yaks/testing'
205
+ * import { shelf } from '@yaks/phone'
206
+ * let numbers = shelf([{ number: '+18885550100', upfront: 1, monthly: 1,
207
+ * currency: 'USD', sms: true, voice: true }])
208
+ * equal((await numbers.find({ tollFree: true })).length, 1)
209
+ * equal((await numbers.buy('+18885550100')).status, 'held')
210
+ * equal(await numbers.find({ tollFree: true }), [])
211
+ * ```
212
+ */
213
+ export let shelf = (offers: Offer[] = []): Shelf => {
214
+ let held = new Set<string>()
215
+ let filed: Shelf['filed'] = new Map()
216
+ let free = (n: string) => /^\+18(00|33|44|55|66|77|88)/.test(n)
217
+ let state = (id: string): Verification => {
218
+ let form = filed.get(id)
219
+ if (!form) throw new Error(`No verification ${id}`)
220
+ return { id, number: form.number, status: form.status, said: form.status }
221
+ }
222
+ return {
223
+ held,
224
+ filed,
225
+ find: (s) =>
226
+ Promise.resolve(
227
+ offers.filter((o) =>
228
+ !held.has(o.number) && (!s.tollFree || free(o.number)) &&
229
+ (!s.number || o.number == s.number) &&
230
+ (!s.area || o.number.startsWith(`+1${s.area}`)) &&
231
+ (!s.region || !!o.place?.endsWith(`, ${s.region}`)) &&
232
+ (!s.contains || o.number.includes(s.contains))
233
+ ).slice(0, s.limit ?? 5),
234
+ ),
235
+ buy: (number) => {
236
+ if (held.has(number) || !offers.some((o) => o.number == number)) {
237
+ return Promise.reject(new Error(`${number} is not on offer`))
238
+ }
239
+ held.add(number)
240
+ return Promise.resolve({
241
+ number,
242
+ order: `order-${held.size}`,
243
+ status: 'held',
244
+ })
245
+ },
246
+ release: (number) => {
247
+ if (!held.delete(number)) {
248
+ return Promise.reject(new Error(`${number} is not held`))
249
+ }
250
+ return Promise.resolve()
251
+ },
252
+ verify: (form) => {
253
+ let id = `verification-${filed.size + 1}`
254
+ filed.set(id, { ...form, status: 'pending' })
255
+ return Promise.resolve(state(id))
256
+ },
257
+ verification: (id) => Promise.resolve().then(() => state(id)),
258
+ }
259
+ }
package/options.js ADDED
@@ -0,0 +1,3 @@
1
+ /** What a config passes to @yaks/phone: which lent texter sends, by name
2
+ * (`stash` keeps texts in memory). */
3
+ //# sourceMappingURL=options.js.map
package/options.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"options.js","sources":["./options.ts"],"names":[],"mappings":"AAAA;oCACoC,GACpC"}