@de./sdk-rn 1.1.7 → 1.2.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.
Files changed (61) hide show
  1. package/dist/allend/Agent/consolidation.d.ts +9 -0
  2. package/dist/allend/Agent/consolidation.d.ts.map +1 -0
  3. package/dist/allend/Agent/consolidation.js +27 -0
  4. package/dist/allend/Agent/consolidation.js.map +1 -0
  5. package/dist/allend/Agent/delivery.d.ts +12 -0
  6. package/dist/allend/Agent/delivery.d.ts.map +1 -0
  7. package/dist/allend/Agent/delivery.js +55 -0
  8. package/dist/allend/Agent/delivery.js.map +1 -0
  9. package/dist/allend/Agent/dispatch.d.ts +9 -0
  10. package/dist/allend/Agent/dispatch.d.ts.map +1 -0
  11. package/dist/allend/Agent/dispatch.js +27 -0
  12. package/dist/allend/Agent/dispatch.js.map +1 -0
  13. package/dist/allend/Agent/index.d.ts +35 -0
  14. package/dist/allend/Agent/index.d.ts.map +1 -0
  15. package/dist/allend/Agent/index.js +74 -0
  16. package/dist/allend/Agent/index.js.map +1 -0
  17. package/dist/allend/Agent/navigation.d.ts +9 -0
  18. package/dist/allend/Agent/navigation.d.ts.map +1 -0
  19. package/dist/allend/Agent/navigation.js +28 -0
  20. package/dist/allend/Agent/navigation.js.map +1 -0
  21. package/dist/allend/Agent/origin.d.ts +10 -0
  22. package/dist/allend/Agent/origin.d.ts.map +1 -0
  23. package/dist/allend/Agent/origin.js +37 -0
  24. package/dist/allend/Agent/origin.js.map +1 -0
  25. package/dist/allend/Agent/reallocation.d.ts +8 -0
  26. package/dist/allend/Agent/reallocation.d.ts.map +1 -0
  27. package/dist/allend/Agent/reallocation.js +17 -0
  28. package/dist/allend/Agent/reallocation.js.map +1 -0
  29. package/dist/allend/Agent/realtime.d.ts +37 -0
  30. package/dist/allend/Agent/realtime.d.ts.map +1 -0
  31. package/dist/allend/Agent/realtime.js +101 -0
  32. package/dist/allend/Agent/realtime.js.map +1 -0
  33. package/dist/allend/Agent/rides.d.ts +13 -0
  34. package/dist/allend/Agent/rides.d.ts.map +1 -0
  35. package/dist/allend/Agent/rides.js +66 -0
  36. package/dist/allend/Agent/rides.js.map +1 -0
  37. package/dist/allend/Agent/shipping.d.ts +12 -0
  38. package/dist/allend/Agent/shipping.d.ts.map +1 -0
  39. package/dist/allend/Agent/shipping.js +55 -0
  40. package/dist/allend/Agent/shipping.js.map +1 -0
  41. package/dist/index.d.ts +4 -1
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +2 -1
  44. package/dist/index.js.map +1 -1
  45. package/dist/utils/index.d.ts +11 -0
  46. package/dist/utils/index.d.ts.map +1 -1
  47. package/dist/utils/index.js +8 -0
  48. package/dist/utils/index.js.map +1 -1
  49. package/package.json +2 -1
  50. package/src/allend/Agent/consolidation.ts +31 -0
  51. package/src/allend/Agent/delivery.ts +61 -0
  52. package/src/allend/Agent/dispatch.ts +31 -0
  53. package/src/allend/Agent/index.ts +102 -0
  54. package/src/allend/Agent/navigation.ts +31 -0
  55. package/src/allend/Agent/origin.ts +42 -0
  56. package/src/allend/Agent/reallocation.ts +18 -0
  57. package/src/allend/Agent/realtime.ts +135 -0
  58. package/src/allend/Agent/rides.ts +73 -0
  59. package/src/allend/Agent/shipping.ts +61 -0
  60. package/src/index.ts +9 -1
  61. package/src/utils/index.ts +20 -1
@@ -0,0 +1,135 @@
1
+ import type { OrderTrackingEvent } from '@de./types'
2
+ import type { RTLocation, Message, Peer } from '../../types'
3
+ import type { SocketAuthCredentials } from '../../types/auth'
4
+ import type { AccessOptions } from '../../types/access'
5
+
6
+ import io, { Socket } from 'socket.io-client'
7
+ import AccessManager from '../Access'
8
+ import baseURL from '../../baseUrl'
9
+
10
+ // ─── Agent Realtime ───────────────────────────────────────────────────────────
11
+ //
12
+ // Socket channel for riders. Two jobs:
13
+ //
14
+ // 1. Order rooms — after `join(jrtoken)` (token from the platform's tracking
15
+ // session, role=agent) the rider streams LOCATION-CHANGE / MESSAGE into
16
+ // the room and receives ORDER_UPDATE events for the order.
17
+ //
18
+ // 2. Proactive-consolidation responses — CONSOLIDATION_ACK / NACK / POSITION
19
+ // go over the socket, NOT the REST respond endpoints (those serve relay
20
+ // and dispatch offers). See de.arch consolidation README: response channels.
21
+ //
22
+ // NOTE — dispatch/consolidation OFFERS do not arrive on this socket: De.
23
+ // emits RALLY_EVENT to service connections only, and the operating platform
24
+ // (e.g. its backend SSE) relays offers to the rider app. Respond to relayed
25
+ // offers via `agent.dispatch.respond(...)` / `agent.consolidation.respond(...)`.
26
+
27
+ export type AgentRealtimeContext = {
28
+ wid: string
29
+ type: string
30
+ xcode: string
31
+ }
32
+
33
+ export default class AgentRealtime extends AccessManager {
34
+ private nsp?: Socket
35
+ private iosHost: string
36
+ private agentId?: string
37
+
38
+ constructor( access: AccessOptions ){
39
+ super( access, 'API' )
40
+
41
+ // Socket.io is served by the same de.arch API server
42
+ const { API_SERVER_BASEURL } = baseURL( access.devHostname )
43
+ this.iosHost = API_SERVER_BASEURL[ access.env || 'dev' ] as string
44
+ }
45
+
46
+ connect( agentId: string ): Promise<void> {
47
+ return new Promise( ( resolve, reject ) => {
48
+ const auth: SocketAuthCredentials = {
49
+ utype: 'agent',
50
+ id: agentId,
51
+ remoteOrigin: this.remoteOrigin as string,
52
+ accessToken: this.accessToken as string
53
+ }
54
+
55
+ this.agentId = agentId
56
+ this.nsp = io( this.iosHost, { auth } )
57
+ this.nsp.on('connect', resolve )
58
+ this.nsp.on('connect_error', reject )
59
+ } )
60
+ }
61
+
62
+ disconnect(){
63
+ this.nsp?.disconnect()
64
+ return true
65
+ }
66
+
67
+ /**
68
+ * Join an order tracking room with a Join Room Token issued by
69
+ * GET /v1/tracking/:reference (role must match this socket's utype)
70
+ */
71
+ join( jrtoken: string ): Promise<boolean> {
72
+ return new Promise( ( resolve, reject ) => {
73
+ this.nsp?.emit('JOIN', jrtoken, ( errmess?: string ) => {
74
+ errmess ? reject( new Error( errmess ) ) : resolve( true )
75
+ } )
76
+ } )
77
+ }
78
+
79
+ // ── Order room streams (after join) ─────────────────────────────────────
80
+
81
+ sendLocation( location: RTLocation ){
82
+ this.nsp?.emit('LOCATION-CHANGE', location )
83
+ return this
84
+ }
85
+
86
+ sendMessage( message: Message ){
87
+ this.nsp?.emit('MESSAGE', message )
88
+ return this
89
+ }
90
+
91
+ onOrderUpdate( fn: ( event: OrderTrackingEvent ) => void ){
92
+ this.nsp?.on('ORDER_UPDATE', fn )
93
+ return this
94
+ }
95
+
96
+ onMessage( fn: ( message: Message ) => void ){
97
+ this.nsp?.on('MESSAGE', fn )
98
+ return this
99
+ }
100
+
101
+ onConnected( fn: ( peer: Peer ) => void ){
102
+ this.nsp?.on('CONNECTED', fn )
103
+ return this
104
+ }
105
+
106
+ onDisconnected( fn: ( peer: Peer ) => void ){
107
+ this.nsp?.on('DISCONNECTED', fn )
108
+ return this
109
+ }
110
+
111
+ // ── Proactive consolidation channel (socket-only responses) ─────────────
112
+
113
+ ackConsolidation( context: AgentRealtimeContext, offerId: string ){
114
+ if( !this.agentId ) throw new Error('Not connected')
115
+ this.nsp?.emit('CONSOLIDATION_ACK', { context, offerId, agentId: this.agentId } )
116
+ return this
117
+ }
118
+
119
+ nackConsolidation( context: AgentRealtimeContext, offerId: string, nackReason?: string ){
120
+ if( !this.agentId ) throw new Error('Not connected')
121
+ this.nsp?.emit('CONSOLIDATION_NACK', { context, offerId, agentId: this.agentId, nackReason } )
122
+ return this
123
+ }
124
+
125
+ /**
126
+ * Stream the rider's position into the proactive consolidation engine —
127
+ * feeds trajectory tracking (ARRIVED / DIVERGING / STATIONARY) for
128
+ * committed handoffs. Distinct from order-room sendLocation().
129
+ */
130
+ positionUpdate( context: AgentRealtimeContext, location: RTLocation, status?: string ){
131
+ if( !this.agentId ) throw new Error('Not connected')
132
+ this.nsp?.emit('CONSOLIDATION_POSITION', { context, agentId: this.agentId, location, status } )
133
+ return this
134
+ }
135
+ }
@@ -0,0 +1,73 @@
1
+ import type {
2
+ AgentRideListValidation,
3
+ AgentRideGetValidation,
4
+ AgentRideAcceptValidation,
5
+ AgentRideStartValidation,
6
+ AgentRideCompleteValidation,
7
+ AgentRideCancelValidation
8
+ } from '@de./types'
9
+ import { qs, type Http, type Res } from '../../utils'
10
+
11
+ // ─── Agent Ride Orders ────────────────────────────────────────────────────────
12
+
13
+ export default class AgentRides {
14
+ constructor( private http: Http ){}
15
+
16
+ async list( querystring?: AgentRideListValidation['querystring'] ): Promise<AgentRideListValidation['response']> {
17
+ const { error, message, data } = await this.http.request<Res<AgentRideListValidation['response']>>({
18
+ url: `/agent/ride/orders${qs( querystring )}`,
19
+ method: 'GET'
20
+ })
21
+ if( error ) throw new Error( message )
22
+ return data
23
+ }
24
+
25
+ async get( reference: string ): Promise<AgentRideGetValidation['response']> {
26
+ const { error, message, data } = await this.http.request<Res<AgentRideGetValidation['response']>>({
27
+ url: `/agent/ride/orders/${reference}`,
28
+ method: 'GET'
29
+ })
30
+ if( error ) throw new Error( message )
31
+ return data
32
+ }
33
+
34
+ async accept( reference: string, body: AgentRideAcceptValidation['body'] ): Promise<AgentRideAcceptValidation['response']> {
35
+ const { error, message, data } = await this.http.request<Res<AgentRideAcceptValidation['response']>>({
36
+ url: `/agent/ride/orders/${reference}/accept`,
37
+ method: 'POST',
38
+ body
39
+ })
40
+ if( error ) throw new Error( message )
41
+ return data
42
+ }
43
+
44
+ async start( reference: string, body: AgentRideStartValidation['body'] ): Promise<AgentRideStartValidation['response']> {
45
+ const { error, message, data } = await this.http.request<Res<AgentRideStartValidation['response']>>({
46
+ url: `/agent/ride/orders/${reference}/start`,
47
+ method: 'POST',
48
+ body
49
+ })
50
+ if( error ) throw new Error( message )
51
+ return data
52
+ }
53
+
54
+ async complete( reference: string, body: AgentRideCompleteValidation['body'] ): Promise<AgentRideCompleteValidation['response']> {
55
+ const { error, message, data } = await this.http.request<Res<AgentRideCompleteValidation['response']>>({
56
+ url: `/agent/ride/orders/${reference}/complete`,
57
+ method: 'POST',
58
+ body
59
+ })
60
+ if( error ) throw new Error( message )
61
+ return data
62
+ }
63
+
64
+ async cancel( reference: string, body: AgentRideCancelValidation['body'] ): Promise<AgentRideCancelValidation['response']> {
65
+ const { error, message, data } = await this.http.request<Res<AgentRideCancelValidation['response']>>({
66
+ url: `/agent/ride/orders/${reference}/cancel`,
67
+ method: 'POST',
68
+ body
69
+ })
70
+ if( error ) throw new Error( message )
71
+ return data
72
+ }
73
+ }
@@ -0,0 +1,61 @@
1
+ import type {
2
+ AgentShippingListValidation,
3
+ AgentShippingGetValidation,
4
+ AgentShippingAcceptValidation,
5
+ AgentShippingUpdateStatusValidation,
6
+ AgentShippingReportIssueValidation
7
+ } from '@de./types'
8
+ import { qs, type Http, type Res } from '../../utils'
9
+
10
+ // ─── Agent Shipping Orders ────────────────────────────────────────────────────
11
+
12
+ export default class AgentShipping {
13
+ constructor( private http: Http ){}
14
+
15
+ async list( querystring?: AgentShippingListValidation['querystring'] ): Promise<AgentShippingListValidation['response']> {
16
+ const { error, message, data } = await this.http.request<Res<AgentShippingListValidation['response']>>({
17
+ url: `/agent/shipping/orders${qs( querystring )}`,
18
+ method: 'GET'
19
+ })
20
+ if( error ) throw new Error( message )
21
+ return data
22
+ }
23
+
24
+ async get( reference: string ): Promise<AgentShippingGetValidation['response']> {
25
+ const { error, message, data } = await this.http.request<Res<AgentShippingGetValidation['response']>>({
26
+ url: `/agent/shipping/orders/${reference}`,
27
+ method: 'GET'
28
+ })
29
+ if( error ) throw new Error( message )
30
+ return data
31
+ }
32
+
33
+ async accept( reference: string ): Promise<AgentShippingAcceptValidation['response']> {
34
+ const { error, message, data } = await this.http.request<Res<AgentShippingAcceptValidation['response']>>({
35
+ url: `/agent/shipping/orders/${reference}/accept`,
36
+ method: 'POST'
37
+ })
38
+ if( error ) throw new Error( message )
39
+ return data
40
+ }
41
+
42
+ async updateStatus( reference: string, body: AgentShippingUpdateStatusValidation['body'] ): Promise<AgentShippingUpdateStatusValidation['response']> {
43
+ const { error, message, data } = await this.http.request<Res<AgentShippingUpdateStatusValidation['response']>>({
44
+ url: `/agent/shipping/orders/${reference}/status`,
45
+ method: 'PATCH',
46
+ body
47
+ })
48
+ if( error ) throw new Error( message )
49
+ return data
50
+ }
51
+
52
+ async reportIssue( reference: string, body: AgentShippingReportIssueValidation['body'] ): Promise<AgentShippingReportIssueValidation['response']> {
53
+ const { error, message, data } = await this.http.request<Res<AgentShippingReportIssueValidation['response']>>({
54
+ url: `/agent/shipping/orders/${reference}/issue`,
55
+ method: 'POST',
56
+ body
57
+ })
58
+ if( error ) throw new Error( message )
59
+ return data
60
+ }
61
+ }
package/src/index.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import Utils from './utils'
2
2
  import MSI, { type MSIInterface, type MSIProps, type MSIRef } from './allend/MSI'
3
3
  import IoTClient, { type IoTClientOptions } from './allend/IoTClient'
4
+ import Agent, { type AgentConfig } from './allend/Agent'
5
+ import { type AgentRealtimeContext } from './allend/Agent/realtime'
4
6
  import Order from './allend/DClient/Order'
5
7
  import Event from './allend/DClient/Event'
6
8
  import Client from './allend/DClient/Client'
@@ -14,8 +16,14 @@ export {
14
16
  type MSIInterface,
15
17
  type MSIProps,
16
18
  type MSIRef,
19
+ Agent,
20
+ type AgentConfig,
21
+ type AgentRealtimeContext,
17
22
  Utils,
18
23
  DClient,
19
24
  IoTClient,
20
25
  type IoTClientOptions
21
- }
26
+ }
27
+
28
+ // Shared contract types — same source of truth as de.arch and @de./sdk
29
+ export type { RallyEvent, RallyEventType, OrderTrackingEvent } from '@de./types'
@@ -1,4 +1,23 @@
1
1
 
2
+ import type { HTTPRequestOptions } from '../types'
3
+
2
4
  import Stream from './stream'
3
5
 
4
- export default { Stream }
6
+ // ─── Requests types ───────────────────────────────────────────────────────────
7
+
8
+ export type Http = { request<T>( opts: HTTPRequestOptions ): Promise<T> }
9
+ export type Res<T = any> = { error: boolean, status?: string, message?: string, data: T }
10
+
11
+ export const qs = ( params?: Record<string, any> ): string => {
12
+ if( !params ) return ''
13
+ const q = new URLSearchParams(
14
+ Object.fromEntries(
15
+ Object.entries( params )
16
+ .filter( ( [, v] ) => v != null )
17
+ .map( ( [k, v] ) => [k, String( v )] )
18
+ )
19
+ ).toString()
20
+ return q ? `?${q}` : ''
21
+ }
22
+
23
+ export default { Stream }