@@ -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
@@ -193,7 +246,7 @@ instance. Transient instances are still retained by the container so
193246
194247String keys are accepted anywhere a token is (` { provide: "logger", useValue } ` )
195248— that is how the legacy facade registers, and how per-call overrides address
196- not-yet-migrated dependencies. New registrations should mint a ` @Contract `
249+ not-yet-migrated dependencies. New registrations should declare a ` @Contract `
197250class, or an ` InjectionToken ` when there is no class to decorate, instead of a
198251new string name.
199252
@@ -217,13 +270,15 @@ Resolution semantics
217270 injector level before delegating to the parent. Both keys index the same
218271 provider record, so re-registering a service by its string name (as plugins
219272 are documented to do with ` $logger ` ) stays visible to ` inject(Logger) `
220- consumers. This holds for ` @Contract ` classes and ` InjectionToken ` s alike.
273+ consumers. This holds for ` @LegacyContract ` classes and ` InjectionToken ` s
274+ alike; a ` @Contract ` class has no name and resolves by identity only.
221275- A leading ` $ ` is stripped from string tokens: ` get("$fs") ` and ` get("fs") `
222276 are the same registration.
223- - The name fallback also makes ** duplicated token copies interchangeable** : if
224- an extension's dependency tree carries its own copy of a contract class or
225- injection token, that copy resolves to the same provider by name. "Works
226- locally, breaks when installed" is not a failure mode of this design.
277+ - The name fallback also makes ** duplicated copies of a named token
278+ interchangeable** : if an extension's dependency tree carries its own copy of a
279+ ` @LegacyContract ` class or injection token, that copy resolves to the same
280+ provider by name. "Works locally, breaks when installed" is not a failure mode
281+ of the tokens ` nativescript/contracts ` exports.
227282- Cyclic dependencies fail with the full resolution path
228283 (` Cyclic dependency detected on dependency 'a'. Resolution path: a -> b -> a ` ).
229284
@@ -267,9 +322,9 @@ $injector.register(provide(DoctorService, DoctorServiceImpl));
267322runInInjectionContext ($injector , () => inject (DoctorService ));
268323```
269324
270- - Legacy string names are permanent: a contract's token name is its interop
271- identity, used by hooks, plugins, and the public API. Nothing is deleted
272- per-service.
325+ - Legacy string names stay while published plugins and hooks depend on them: a
326+ named contract's name is its interop identity, used by hooks, plugins, and the
327+ public API. Nothing is deleted per-service.
273328- Every legacy member (` resolve ` , ` register ` , ` require* ` , the command-registry
274329 surface) carries ` @deprecated ` JSDoc naming its replacement.
275330- Legacy usage at the external entry points (param-name hooks, require-time
0 commit comments