Mercurial > hg > nginx-site
annotate xml/en/docs/njs/typescript.xml @ 2983:67dd348c9757
Documented new global nginx properties in njs Reference.
The following properties were added: ngx.build, ngx.conf_file_path,
ngx.error_log_path, ngx.prefix, ngx.version, ngx.version_number.
author | Yaroslav Zhuravlev <yar@nginx.com> |
---|---|
date | Thu, 01 Jun 2023 17:12:18 +0100 |
parents | 0e6bbd8138c4 |
children |
rev | line source |
---|---|
2541
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
1 <?xml version="1.0"?> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
2 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
3 <!-- |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
4 Copyright (C) Nginx, Inc. |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
5 --> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
6 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
8 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
9 <article name="Writing njs code using TypeScript definition files" |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
10 link="/en/docs/njs/typescript.html" |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
11 lang="en" |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
12 rev="1"> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
13 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
14 <section> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
15 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
16 <para> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
17 <link url="https://www.typescriptlang.org">TypeScript</link> is |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
18 a typed superset of JavaScript |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
19 that compiles to plain JavaScript. |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
20 </para> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
21 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
22 <para> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
23 TypeScript supports definition files that contain |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
24 type information of existing JavaScript libraries. |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
25 This enables other programs to use the values defined in the files |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
26 as if they were statically typed TypeScript entities. |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
27 </para> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
28 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
29 <para> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
30 njs provides TypeScript definition files for its |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
31 <link doc="reference.xml">API</link> which can be used to: |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
32 <list type="bullet"> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
33 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
34 <listitem> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
35 Get autocompletion and API check in an editor |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
36 </listitem> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
37 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
38 <listitem> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
39 Write njs type-safe code |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
40 </listitem> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
41 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
42 </list> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
43 </para> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
44 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
45 </section> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
46 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
47 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
48 <section id="get" name="Compiling TypeScript definition files"> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
49 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
50 <para> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
51 <example> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
52 $ hg clone http://hg.nginx.org/njs |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
53 $ cd njs && ./configure && make ts |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
54 $ ls build/ts/ |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
55 njs_core.d.ts |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
56 njs_shell.d.ts |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
57 ngx_http_js_module.d.ts |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
58 ngx_stream_js_module.d.ts |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
59 </example> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
60 </para> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
61 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
62 </section> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
63 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
64 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
65 <section id="autocomplete" name="API checks and autocompletions"> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
66 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
67 <para> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
68 Put <literal>*.d.ts</literal> files to a place where you editor can find it. |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
69 </para> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
70 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
71 <para> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
72 <literal>test.js</literal>: |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
73 <example> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
74 /// <reference path="ngx_http_js_module.d.ts" /> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
75 /** |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
76 * @param {NginxHTTPRequest} r |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
77 * */ |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
78 function content_handler(r) { |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
79 r.headersOut['content-type'] = 'text/plain'; |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
80 r.return(200, "Hello"); |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
81 } |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
82 </example> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
83 </para> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
84 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
85 </section> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
86 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
87 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
88 <section id="write" name="Writing njs type-safe code"> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
89 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
90 <para> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
91 <literal>test.ts</literal>: |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
92 <example> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
93 /// <reference path="ngx_http_js_module.d.ts" /> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
94 function content_handler(r: NginxHTTPRequest) { |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
95 r.headersOut['content-type'] = 'text/plain'; |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
96 r.return(200, "Hello from TypeScript"); |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
97 } |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
98 </example> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
99 TypeScript installation: |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
100 <example> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
101 # npm install -g typescript |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
102 </example> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
103 TypeScript compilation: |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
104 <example> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
105 $ tsc test.ts |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
106 $ cat test.js |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
107 </example> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
108 The resulting <literal>test.js</literal> file can be used directly with njs. |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
109 </para> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
110 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
111 </section> |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
112 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
113 |
0e6bbd8138c4
Added article about TypeScript in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
114 </article> |