Share code with an Angular web app
Keep your stores and services in one library that both your Angular web app and your AngularLynx app import. Extend @blotch/angular-lynx/tsconfig.shared.json in that library, and TypeScript flags a browser-only API before it crashes on a phone.
Set up the library
Give the shared library a tsconfig.json of its own that extends the preset:
Check it with npx tsc -p libs/shared, and add that command to CI or to your lint script.
- Nothing else applies the preset.
ng build,ng serveandng testcompile the library's files with each app's own configuration, as before, so a browser-only API in the library only shows up when its owntsc -pruns. Without that step, the preset does nothing. - The preset sets
libandtypes. They hold only what Lynx's main thread, where Angular runs, has. TypeScript'sdomlibrary isn't in them, sodocument,windowandlocalStorageare type errors. - It needs
moduleResolutionset tobundler, as above and asng newsets it, so TypeScript finds the preset's types through the package's exports.node16works too, with"module": "Node16", which it requires.node(node10) finds the preset's types, but not Angular's own entry points such as@angular/common/http, and TypeScript 6 reports it as deprecated unless you set"ignoreDeprecations": "6.0". TypeScript 7 removes it. - The preset turns on
skipLibCheck. TypeScript then skips every declaration file, including the library's own.d.tsfiles, not just those innode_modules. Angular's declarations name DOM types the preset leaves out, so turning it off reports errors in them. @blotch/angular-lynxmust resolve from the library's folder. In a workspace, the AngularLynx app's dependency is enough.
Write a store
A store written against the preset runs in both apps:
Use HttpClient and httpResource
Each app adds provideHttpClient() to its own config. The library only injects HttpClient or calls httpResource, which work the same in both apps. See HTTP requests.
Give every request an absolute URL. A Lynx app has no page URL to resolve /api/todos against. Provide the server's address from each app, as API_URL above:
What the library can use
Everything below works on the phone and in a current browser. Where Lynx's version does less, the preset's types do less too.
What to keep out of it
These are type errors under the preset. Keep them in the app that has them, or put them behind a service each app provides.
- The DOM:
document,window,navigator, elements. Lynx'sdocumentandwindoware stand-ins, not a DOM. - Storage:
localStorageandsessionStorage. Declare an abstract class in the library, and provide it from each app, withlocalStorageon the web andLynxLocalStorageon the phone. FormData: Lynx has none.XMLHttpRequest: it exists, but useHttpClientorfetch.- A bare
Uint8Arrayas aBlobpart or a request body: the DOM's types take only a view of anArrayBufferthere, so type itUint8Array<ArrayBuffer>.TextEncoder.encode()returns one. - Fetch options Lynx doesn't apply:
redirectother than'follow',modeother than'cors', andintegrity. On the phone Lynx's HTTP service follows every redirect, makes no CORS checks and checks no integrity, so a library that counts on them to refuse a response would take it there.cache,credentials,keepalive,priority,referrerandreferrerPolicytype-check, since a browser applies them, but don't change the request on a phone.HttpClient's own options of the same names aren't checked: Angular types them as any string. Keep a call that needs one in the web app. If it has to stay in the library and only ever runs on the web, put// @ts-ignorewith the reason above the option: the web app compiles the same file and accepts the line, so a// @ts-expect-errorthere would be an unused directive, which is an error too. - Newer JavaScript the engine can't add:
new Error(message, { cause }), resizableArrayBuffers,Float16ArrayandAtomics.
Storage behind a service
The web app provides its own class, which reads and writes localStorage.
What the types can't catch
Intl. TypeScript's base library declares it, so the preset can't leave it out, but Lynx has it only when you add it with theintlbuild option. Without it,toLocaleString()ignores the locale.- The
dandvregular expression flags. TypeScript checks them against yourtarget, notlib. - Error's
causethrough loosely typed arguments.new Error(message, { cause })is a type error, butnew Error(...args)withargs: any[]isn't. - A global reached by a name built at runtime, such as
globalThis['fe' + 'tch']orglobalThis[name]. See Added only when used.
Use a DOM API on purpose
To type a library against the DOM again, set lib and types in its own tsconfig.json. They replace the preset's:
For one API, keep the preset and leave that code in the app that has it, behind a service the library declares, as with storage above.
Related
- The JavaScript environment: everything the main thread has, and what's missing
- HTTP requests
- Coming from web Angular
