@nativescript/compiler-angular 0.0.0-stage → 9.2.0-alpha.9

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.
@@ -0,0 +1,172 @@
1
+ package org.nativescript.kit
2
+
3
+ // Angular's dependency injection and the @nativescript/angular services an app injects, for components and
4
+ // services compiled to classes with a `shared` instance (`providedIn: 'root'`), as kit-apple's AngularAPI.swift.
5
+
6
+ /** A component class used as a value (`dialog.open(SheetComponent)`): what creating and rendering it gives. */
7
+ class ComponentFactory(internal val make: () -> View)
8
+
9
+ /** `NativeDialogRef`: the open dialog a component was created in. */
10
+ class NativeDialogRef {
11
+ /** `NATIVE_DIALOG_DATA`: the `data` the dialog was opened with. */
12
+ var data: Any? = null
13
+ internal set
14
+ private val closed = RxReplaySubject<Any?>(1.0)
15
+
16
+ fun close(result: Any? = null) = Modal.close(result)
17
+ fun afterClosed(): RxObservable<Any?> = closed.asObservable()
18
+ internal fun didClose(result: Any?) = closed.next(result)
19
+
20
+ companion object {
21
+ /** The dialog whose component is being created; one no dialog opened before then. */
22
+ var current = NativeDialogRef()
23
+ }
24
+ }
25
+
26
+ /** `NativeDialog` (`NativeDialogService`): a component shown as a native modal, with `nativeOptions` as `showModal` takes them. */
27
+ class NativeDialogService private constructor() {
28
+ fun open(component: Any?, config: Any? = null): NativeDialogRef {
29
+ val ref = NativeDialogRef()
30
+ ref.data = jsGet(config, "data")
31
+ val factory = jsBox(component) as? ComponentFactory ?: return ref
32
+ Modal.show(jsGet(config, "nativeOptions"), closeCallback = { result -> ref.didClose(result) }) {
33
+ val previous = NativeDialogRef.current
34
+ NativeDialogRef.current = ref
35
+ try { factory.make() } finally { NativeDialogRef.current = previous }
36
+ }
37
+ return ref
38
+ }
39
+
40
+ companion object {
41
+ val shared = NativeDialogService()
42
+ }
43
+ }
44
+
45
+ /**
46
+ * `HttpClient` through core's `Http.request`, the request @nativescript/angular's backend sends through
47
+ * core's XMLHttpRequest: the body parsed as JSON unless `responseType` is `'text'`, a status of 400 or more an error.
48
+ */
49
+ class HttpClient private constructor() {
50
+ private fun send(method: String, url: String, body: Any? = null, options: Any? = null): RxObservable<Any?> {
51
+ val flatOpts = jsBox(options)
52
+ val text = jsToString(jsGetOptional(flatOpts, "responseType")) == "text"
53
+ val observe = jsToString(jsGetOptional(flatOpts, "observe") ?: "body")
54
+ val rawHeaders = jsGetOptional(flatOpts, "headers")
55
+ val rawParams = jsGetOptional(flatOpts, "params")
56
+
57
+ var fullUrl = url
58
+ if (!jsIsNullish(rawParams)) {
59
+ val q = (jsBox(rawParams) as? HttpParams)?.toString() ?: HttpParams(rawParams).toString()
60
+ if (q.isNotEmpty()) {
61
+ fullUrl = if (fullUrl.contains("?")) "$fullUrl&$q" else "$fullUrl?$q"
62
+ }
63
+ }
64
+
65
+ var headersObj: Any? = (jsBox(rawHeaders) as? HttpHeaders)?.toJSObject() ?: rawHeaders
66
+ val payloadBody = body ?: jsGetOptional(flatOpts, "body")
67
+ val upperMethod = method.uppercase()
68
+
69
+ val request: () -> JSPromise<Any?> = {
70
+ val failure = { status: Double, reason: String -> JSError("Http failure response for $fullUrl: ${jsToString(status)} $reason") }
71
+ val (promise, resolvers) = JSPromise.pending<Any?>()
72
+ try {
73
+ val settings = JSObject("url" to fullUrl, "method" to upperMethod)
74
+ if (!jsIsNullish(payloadBody) && upperMethod != "GET" && upperMethod != "HEAD") {
75
+ if (payloadBody is String) {
76
+ settings["content"] = payloadBody
77
+ } else {
78
+ val json = jsJSONStringify(payloadBody)
79
+ if (json is String) {
80
+ settings["content"] = json
81
+ if (jsIsNullish(headersObj)) {
82
+ headersObj = JSObject("Content-Type" to "application/json")
83
+ }
84
+ }
85
+ }
86
+ }
87
+ if (!jsIsNullish(headersObj)) settings["headers"] = headersObj
88
+ (Core_http_http_request_index.request(settings) ?: jsCannotRead("then")).then({ response: Any? ->
89
+ try {
90
+ val status = jsToNumber(jsGet(response, "statusCode"))
91
+ val reason = if (status == 0.0) "Unknown Error" else reasonPhrase(status.toInt())
92
+ if (status >= 400 || status == 0.0) {
93
+ resolvers.reject(failure(status, reason))
94
+ } else {
95
+ val rawContent = jsToString(jsCallMethod(jsGet(response, "content"), "toString"))
96
+ val parsed: Any? = if (text) rawContent else if (rawContent.isEmpty()) null else jsJSONParse(rawContent)
97
+ if (observe == "response") {
98
+ resolvers.resolve(HttpResponse(JSObject(
99
+ "body" to parsed,
100
+ "status" to status,
101
+ "statusText" to reason,
102
+ "url" to fullUrl,
103
+ )))
104
+ } else {
105
+ resolvers.resolve(parsed)
106
+ }
107
+ }
108
+ } catch (e: Throwable) {
109
+ resolvers.reject(jsCaught(e))
110
+ }
111
+ }, { _: Any? -> resolvers.reject(failure(0.0, "Unknown Error")) })
112
+ } catch (e: Throwable) {
113
+ resolvers.reject(jsCaught(e))
114
+ }
115
+ promise
116
+ }
117
+ val observable = RxObservable.ending<Any?> { next, error, complete ->
118
+ val subscription = TeardownHandle()
119
+ request().then({ value ->
120
+ if (!subscription.closed) {
121
+ next(value)
122
+ complete()
123
+ }
124
+ }, { err -> if (!subscription.closed) error(err) })
125
+ subscription
126
+ }
127
+ observable.first = request
128
+ return observable
129
+ }
130
+
131
+ fun request(first: Any?, url: String? = null, options: Any? = null): RxObservable<Any?> {
132
+ val req = jsBox(first) as? HttpRequest
133
+ if (req != null) {
134
+ return send(req.method, req.url, req.body, JSObject(
135
+ "headers" to req.headers,
136
+ "params" to req.params,
137
+ "responseType" to req.responseType,
138
+ "observe" to "response",
139
+ ))
140
+ }
141
+ return send(jsToString(first), url ?: "", null, options)
142
+ }
143
+
144
+ fun get(url: String, options: Any? = null): RxObservable<Any?> = send("GET", url, null, options)
145
+ fun delete(url: String, options: Any? = null): RxObservable<Any?> = send("DELETE", url, null, options)
146
+ fun head(url: String, options: Any? = null): RxObservable<Any?> = send("HEAD", url, null, options)
147
+ fun options(url: String, options: Any? = null): RxObservable<Any?> = send("OPTIONS", url, null, options)
148
+ fun post(url: String, body: Any? = null, options: Any? = null): RxObservable<Any?> = send("POST", url, body, options)
149
+ fun put(url: String, body: Any? = null, options: Any? = null): RxObservable<Any?> = send("PUT", url, body, options)
150
+ fun patch(url: String, body: Any? = null, options: Any? = null): RxObservable<Any?> = send("PATCH", url, body, options)
151
+
152
+ fun jsonp(url: String, callbackParam: String): RxObservable<Any?> {
153
+ val sep = if (url.contains("?")) "&" else "?"
154
+ return get("$url$sep${jsEncodeURIComponent(callbackParam)}=JSONP_CALLBACK")
155
+ }
156
+
157
+ private fun reasonPhrase(status: Int): String = when (status) {
158
+ 200 -> "OK"; 201 -> "Created"; 202 -> "Accepted"; 204 -> "No Content"
159
+ 400 -> "Bad Request"; 401 -> "Unauthorized"; 403 -> "Forbidden"; 404 -> "Not Found"; 405 -> "Method Not Allowed"
160
+ 408 -> "Request Timeout"; 409 -> "Conflict"; 410 -> "Gone"; 422 -> "Unprocessable Entity"; 429 -> "Too Many Requests"
161
+ 500 -> "Internal Server Error"; 501 -> "Not Implemented"; 502 -> "Bad Gateway"; 503 -> "Service Unavailable"; 504 -> "Gateway Timeout"
162
+ else -> ""
163
+ }
164
+
165
+ companion object {
166
+ val shared = HttpClient()
167
+ }
168
+ }
169
+
170
+ typealias KitHttpClient = HttpClient
171
+ typealias KitNativeDialogRef = NativeDialogRef
172
+ typealias KitNativeDialogService = NativeDialogService