@@ -37,13 +37,31 @@ Tokens: `@Contract`
3737-------------------
3838
3939A token is an abstract class annotated with ` @Contract ` . The class is both the
40- compile-time type and the runtime lookup key; the decorator records the token's
41- canonical string name:
40+ compile-time type and the runtime lookup key:
4241
4342``` ts
4443import { Contract } from " nativescript/contracts" ;
4544
46- @Contract ({ name: " doctorService" }) // canonical form, no `$`
45+ @Contract ()
46+ export abstract class ReportWriter {
47+ abstract write(report : string ): Promise <void >;
48+ }
49+ ```
50+
51+ Such a token resolves by the class and nothing else: it has no string spelling,
52+ and a duplicated copy of the class is a different token.
53+
54+ Implementations do not become tokens by extending or implementing a contract;
55+ only the decorated class itself is a token.
56+
57+ ### Named contracts: ` @LegacyContract `
58+
59+ ` @LegacyContract ` adds a canonical string name, which the token also answers to:
60+
61+ ``` ts
62+ import { LegacyContract } from " nativescript/contracts" ;
63+
64+ @LegacyContract ({ name: " doctorService" }) // canonical form, no `$`
4765export abstract class DoctorService {
4866 abstract canExecuteLocalBuild(configuration ? : {
4967 platform? : string ;
@@ -52,6 +70,15 @@ export abstract class DoctorService {
5270}
5371```
5472
73+ A name exists for compatibility. Use one only when the service has to stay
74+ reachable by callers that cannot hold the class:
75+
76+ - code that resolves it by string — hook parameters, ` $ ` -prefixed constructor
77+ parameters, ` resolve("name") ` , string registrations;
78+ - an extension whose dependency tree carries its own copy of the contract. This
79+ is why the contracts exported from ` nativescript/contracts ` are named, the
80+ recent ones included: without a name, such a copy cannot be resolved.
81+
5582Rules:
5683
5784- ** The name is an explicit string literal** — never derived from ` class.name ` ,
@@ -60,8 +87,34 @@ Rules:
6087 same name ** throws at load time** , because a duplicate would silently alias
6188 two tokens.
6289- The options object leaves room for future fields without changing call sites.
63- - Implementations do not become tokens by extending or implementing a contract;
64- only the decorated class itself is a token.
90+
91+ ` @Contract({ name }) ` is the deprecated spelling of ` @LegacyContract({ name }) `
92+ and behaves identically.
93+
94+ ### Internal contracts
95+
96+ Only what ` nativescript/contracts ` exports is a supported API. Contracts for the
97+ CLI's own plumbing live in ` lib/contracts/internal ` and are never exported from
98+ it: they may change or disappear in any release, even where a name keeps them
99+ resolvable by string or a deep ` require() ` reaches the file. A public contract's
100+ signature must not mention an internal one.
101+
102+ To move an existing service onto an internal contract:
103+
104+ 1 . Declare the contract in ` lib/contracts/internal ` , as a ` @LegacyContract `
105+ under the name the service is registered with.
106+ 2 . Turn the ambient interface into an alias of it
107+ (` interface IOsInfo extends OsInfo {} ` ).
108+ 3 . Rename the implementation to ` *Impl ` and register it with
109+ ` injector.register(provide(OsInfo, OsInfoImpl)) ` — or by its name, while it
110+ still takes ` $ ` -prefixed constructor parameters. The ` injector.require() `
111+ entry in the bootstrap stays as it is.
112+ 4 . Move consumers from ` $osInfo ` parameters to ` inject(OsInfo) ` fields, in as
113+ many steps as convenient: both reach the same instance.
114+
115+ ` lib/contracts/internal/os-info.ts ` is the worked example. A service that never
116+ had a string name takes a plain ` @Contract() ` and a ` provideLazy() ` registration
117+ instead.
65118
66119Tokens for non-classes: ` InjectionToken `
67120----------------------------------------
@@ -81,12 +134,12 @@ class ProjectPatcher {
81134}
82135```
83136
84- The description ** is** the registry name, exactly as a contract's name is, so a
137+ The description ** is** the registry name, exactly as a named contract's is, so a
85138token is a typed alias onto an existing registration and nothing has to change
86139where the value is registered — ` injector.register("xcode", xcode) ` keeps
87140serving ` inject(XCODE) ` . A leading ` $ ` in the description is stripped.
88141
89- Names are minted in the same registry ` @Contract ` uses, so a token and a
142+ Names are minted in the same registry ` @LegacyContract ` uses, so a token and a
90143contract cannot claim the same name: the second one ** throws at load time** ,
91144rather than silently aliasing one registration under two tokens.
92145
@@ -213,7 +266,7 @@ instance. Transient instances are still retained by the container so
213266
214267String keys are accepted anywhere a token is (` { provide: "logger", useValue } ` )
215268— that is how the legacy facade registers, and how per-call overrides address
216- not-yet-migrated dependencies. New registrations should mint a ` @Contract `
269+ not-yet-migrated dependencies. New registrations should declare a ` @Contract `
217270class, or an ` InjectionToken ` when there is no class to decorate, instead of a
218271new string name.
219272
@@ -237,13 +290,15 @@ Resolution semantics
237290 injector level before delegating to the parent. Both keys index the same
238291 provider record, so re-registering a service by its string name (as plugins
239292 are documented to do with ` $logger ` ) stays visible to ` inject(Logger) `
240- consumers. This holds for ` @Contract ` classes and ` InjectionToken ` s alike.
293+ consumers. This holds for ` @LegacyContract ` classes and ` InjectionToken ` s
294+ alike; a ` @Contract ` class has no name and resolves by identity only.
241295- A leading ` $ ` is stripped from string tokens: ` get("$fs") ` and ` get("fs") `
242296 are the same registration.
243- - The name fallback also makes ** duplicated token copies interchangeable** : if
244- an extension's dependency tree carries its own copy of a contract class or
245- injection token, that copy resolves to the same provider by name. "Works
246- locally, breaks when installed" is not a failure mode of this design.
297+ - The name fallback also makes ** duplicated copies of a named token
298+ interchangeable** : if an extension's dependency tree carries its own copy of a
299+ ` @LegacyContract ` class or injection token, that copy resolves to the same
300+ provider by name. "Works locally, breaks when installed" is not a failure mode
301+ of the tokens ` nativescript/contracts ` exports.
247302- Cyclic dependencies fail with the full resolution path
248303 (` Cyclic dependency detected on dependency 'a'. Resolution path: a -> b -> a ` ).
249304
@@ -287,9 +342,9 @@ $injector.register(provide(DoctorService, DoctorServiceImpl));
287342runInInjectionContext ($injector , () => inject (DoctorService ));
288343```
289344
290- - Legacy string names are permanent: a contract's token name is its interop
291- identity, used by hooks, plugins, and the public API. Nothing is deleted
292- per-service.
345+ - Legacy string names stay while published plugins and hooks depend on them: a
346+ named contract's name is its interop identity, used by hooks, plugins, and the
347+ public API. Nothing is deleted per-service.
293348- Every legacy member (` resolve ` , ` register ` , ` require* ` , the command-registry
294349 surface) carries ` @deprecated ` JSDoc naming its replacement.
295350- Legacy usage at the external entry points (param-name hooks, require-time
0 commit comments