Skip to content

Commit dc83f4e

Browse files
authored
feat: improve alpha types support (#9256)
* chore: make return types explicit * chore: improve module processing * fix lint
1 parent 7127828 commit dc83f4e

File tree

6 files changed

+27
-20
lines changed

6 files changed

+27
-20
lines changed

packages/model/src/-private/legacy-relationships-support.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import type { CollectionRelationship } from '@warp-drive/core-types/cache/relati
2727
import type { LocalRelationshipOperation } from '@warp-drive/core-types/graph';
2828
import type { CollectionResourceRelationship, SingleResourceRelationship } from '@warp-drive/core-types/spec/raw';
2929

30+
import type { ManyArray } from '../-private';
3031
import RelatedCollection from './many-array';
3132
import type { MinimalLegacyRecord } from './model-methods';
3233
import type { BelongsToProxyCreateArgs, BelongsToProxyMeta } from './promise-belongs-to';
@@ -298,11 +299,11 @@ export class LegacySupport {
298299
assert('hasMany only works with the @ember-data/json-api package');
299300
}
300301

301-
reloadHasMany(key: string, options?: BaseFinderOptions) {
302+
reloadHasMany<T>(key: string, options?: BaseFinderOptions): Promise<ManyArray<T>> | PromiseManyArray<T> {
302303
if (HAS_JSON_API_PACKAGE) {
303304
const loadingPromise = this._relationshipPromisesCache[key];
304305
if (loadingPromise) {
305-
return loadingPromise;
306+
return loadingPromise as Promise<ManyArray<T>>;
306307
}
307308
const relationship = this.graph.get(this.identifier, key) as CollectionEdge;
308309
const { definition, state } = relationship;
@@ -313,10 +314,10 @@ export class LegacySupport {
313314
const promise = this.fetchAsyncHasMany(key, relationship, manyArray, options);
314315

315316
if (this._relationshipProxyCache[key]) {
316-
return this._updatePromiseProxyFor('hasMany', key, { promise });
317+
return this._updatePromiseProxyFor('hasMany', key, { promise }) as PromiseManyArray<T>;
317318
}
318319

319-
return promise;
320+
return promise as Promise<ManyArray<T>>;
320321
}
321322
assert(`hasMany only works with the @ember-data/json-api package`);
322323
}
@@ -388,7 +389,9 @@ export class LegacySupport {
388389
return promiseProxy;
389390
}
390391

391-
referenceFor(kind: string | null, name: string) {
392+
referenceFor(kind: 'belongsTo', name: string): BelongsToReference;
393+
referenceFor(kind: 'hasMany', name: string): HasManyReference;
394+
referenceFor(kind: 'belongsTo' | 'hasMany', name: string) {
392395
let reference = this.references[name];
393396

394397
if (!reference) {

packages/model/src/-private/many-array.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,10 @@ export default class RelatedCollection<T = unknown> extends RecordArray<T> {
390390
/**
391391
Reloads all of the records in the manyArray. If the manyArray
392392
holds a relationship that was originally fetched using a links url
393-
Ember Data will revisit the original links url to repopulate the
393+
EmberData will revisit the original links url to repopulate the
394394
relationship.
395395
396-
If the manyArray holds the result of a `store.query()` reload will
396+
If the ManyArray holds the result of a `store.query()` reload will
397397
re-run the original query.
398398
399399
Example
@@ -409,9 +409,9 @@ export default class RelatedCollection<T = unknown> extends RecordArray<T> {
409409
@method reload
410410
@public
411411
*/
412-
reload(options?: BaseFinderOptions) {
412+
reload(options?: BaseFinderOptions): Promise<this> {
413413
// TODO this is odd, we don't ask the store for anything else like this?
414-
return this._manager.reloadHasMany(this.key, options);
414+
return this._manager.reloadHasMany<T>(this.key, options) as Promise<this>;
415415
}
416416

417417
/**

packages/model/src/-private/model-methods.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { RecordStore } from '@warp-drive/core-types/symbols';
1313
import type Errors from './errors';
1414
import { lookupLegacySupport } from './legacy-relationships-support';
1515
import type RecordState from './record-state';
16+
import type BelongsToReference from './references/belongs-to';
17+
import type HasManyReference from './references/has-many';
1618

1719
export interface MinimalLegacyRecord {
1820
errors: Errors;
@@ -51,11 +53,11 @@ export function unloadRecord(this: MinimalLegacyRecord) {
5153
this[RecordStore].unloadRecord(this);
5254
}
5355

54-
export function belongsTo(this: MinimalLegacyRecord, prop: string) {
56+
export function belongsTo(this: MinimalLegacyRecord, prop: string): BelongsToReference {
5557
return lookupLegacySupport(this).referenceFor('belongsTo', prop);
5658
}
5759

58-
export function hasMany(this: MinimalLegacyRecord, prop: string) {
60+
export function hasMany(this: MinimalLegacyRecord, prop: string): HasManyReference {
5961
return lookupLegacySupport(this).referenceFor('hasMany', prop);
6062
}
6163

packages/model/src/-private/promise-belongs-to.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import type { TypeFromInstanceOrString } from '@warp-drive/core-types/record';
1010

1111
import type { LegacySupport } from './legacy-relationships-support';
1212
import { PromiseObject } from './promise-proxy-base';
13-
import type BelongsToReference from './references/belongs-to';
1413

1514
export interface BelongsToProxyMeta<T = unknown> {
1615
key: string;
@@ -51,9 +50,9 @@ class PromiseBelongsTo<T = unknown> extends Extended<T> {
5150
declare _belongsToState: BelongsToProxyMeta<T>;
5251

5352
@cached
54-
get id() {
53+
get id(): string | null {
5554
const { key, legacySupport } = this._belongsToState;
56-
const ref = legacySupport.referenceFor('belongsTo', key) as BelongsToReference;
55+
const ref = legacySupport.referenceFor('belongsTo', key);
5756

5857
return ref.id();
5958
}

packages/model/src/-private/references/has-many.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -743,11 +743,11 @@ export default class HasManyReference<
743743
@param {Object} options the options to pass in.
744744
@return {Promise} a promise that resolves with the ManyArray in this has-many relationship.
745745
*/
746-
reload(options?: BaseFinderOptions) {
746+
reload(options?: BaseFinderOptions): Promise<ManyArray<Related>> {
747747
const support: LegacySupport = (LEGACY_SUPPORT as Map<StableRecordIdentifier, LegacySupport>).get(
748748
this.___identifier
749749
)!;
750-
return support.reloadHasMany(this.key, options);
750+
return support.reloadHasMany(this.key, options) as Promise<ManyArray<Related>>;
751751
}
752752
}
753753
defineSignal(HasManyReference.prototype, '_ref', 0);

release/core/publish/steps/generate-tarballs.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { APPLIED_STRATEGY, Package } from '../../../utils/package';
44
import path from 'path';
55
import fs from 'fs';
66
import { Glob } from 'bun';
7-
import { getFile } from '../../../utils/json-file';
87

98
const PROJECT_ROOT = process.cwd();
109
const TARBALL_DIR = path.join(PROJECT_ROOT, 'tmp/tarballs');
@@ -176,9 +175,13 @@ async function convertFileToModule(fileData: string, relativePath: string, pkgNa
176175
lines[i] = lines[i].replace(/^declare /, '').replaceAll(' declare ', ' ');
177176
const line = lines[i];
178177

178+
if (line.includes(`import(".`) || line.includes(`import('.`)) {
179+
throw new Error(`Unhandled Dynamic Relative Import in ${relativePath}`);
180+
}
181+
179182
if (line.startsWith('import ')) {
180183
if (!line.includes(`'`)) {
181-
throw new Error(`Unhandled import in ${relativePath}`);
184+
throw new Error(`Unhandled Import in ${relativePath}`);
182185
}
183186
if (line.includes(`'.`)) {
184187
const importPath = line.match(/'([^']+)'/)![1];
@@ -190,7 +193,7 @@ async function convertFileToModule(fileData: string, relativePath: string, pkgNa
190193
// fix re-exports
191194
else if (line.startsWith('export {')) {
192195
if (!line.includes('}')) {
193-
throw new Error(`Unhandled re-export in ${relativePath}`);
196+
throw new Error(`Unhandled Re-export in ${relativePath}`);
194197
}
195198
if (line.includes(`'.`)) {
196199
const importPath = line.match(/'([^']+)'/)![1];
@@ -202,7 +205,7 @@ async function convertFileToModule(fileData: string, relativePath: string, pkgNa
202205
// fix * re-exports
203206
else if (line.startsWith('export * from')) {
204207
if (!line.includes(`'`)) {
205-
throw new Error(`Unhandled re-export in ${relativePath}`);
208+
throw new Error(`Unhandled Re-export in ${relativePath}`);
206209
}
207210
if (line.includes(`'.`)) {
208211
const importPath = line.match(/'([^']+)'/)![1];

0 commit comments

Comments
 (0)